aspose file tools*
The moose likes Testing and the fly likes How can I check if a particular value is present in an Array using assertTrue ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How can I check if a particular value is present in an Array using assertTrue ??" Watch "How can I check if a particular value is present in an Array using assertTrue ??" New topic
Author

How can I check if a particular value is present in an Array using assertTrue ??

Pooja Patole
Ranch Hand

Joined: Mar 13, 2012
Posts: 35
Hello Everyone,

I have a doubt. I have an Array which have some data suppose PriceData1 , PriceData2, PriceData3, ElectricData0,ElectricData1 and so on. I want to check whether all these values are present in the array or not.. So I think using assertTrue I can check it but am not sure how.. Please help..

Thanks
Pooja
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Pooja,

There are a number of ways you could approach it. Some will depend on how well you know how the values are stored in the array as well as how efficient you want the test to be. For example, if you expect PriceData1 to be in the 1st position then you would be able to to a simple assertEquals(PriceData1, arrayVariable[0])..If you want to make sure the value PriceData1 is present in some position in the array, then you could write a helper method to iterate through the array looking for a value and return true when found or false if not found and then have an assertTrue call performed on the reply of that helper method.

Depending on the number of values you are seeking to verify, you could consider organizing them in a HashMap in a class encapsulating the value and a boolean variable "found". For each value in your test result array, perform the keyed lookup on that value in the HashMap and set the value to true. After iterating through the array, iterate through the entries in the hashmap and assert that 'found' is true.

I guess this also brings up questions like what happens if PriceData1 appears twice in the array? If this is an error, then something like the hashmap lookup would enable you to fail the test when that value is already "found". Another question is how do you handle data in the test result array that is not on your expected value list. Is this ok, or is this another instance of where you'd want to code the test to fail.
 
wood burning stoves
 
subject: How can I check if a particular value is present in an Array using assertTrue ??