I am trying to assert that a given array contains at least one instance of a given element. Is there an assert method that already does this? If so which one?
I am using Java6 and JUnit3.
For primitive values, use the array. includes() method to check if an array contains a value. For objects, use the isEqual() helper function to compare objects and array. some() method to check if the array contains the object.
String Arrays The simplest and easiest way to check if a string array contains a certain value is the following: Convert the array into a list. Use the List. contains() method to check if the value exists in the list.
You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.
You can cast the array to a list:
assertTrue(Arrays.asList(yourArray).contains(yourElement));
assertThat(Arrays.asList(yourArray), hasItem(yourElement));
This will give you fine-grained information in the event of a test failure. It will print out your element and the collection it's looking in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With