I'm testing a method to see if it returns the correct string. This string is made up of a lot of lines whose order might change, thus usually giving 2 possible combinations. That order is not important for my application.
However, because the order of the lines might change, writing just an Assert statement will not work, since sometimes it will pass the test, and sometimes it will fail the test.
So, is it possible to write a test that will assert an actual string value against 2 or more expected string values and see if it is equal to any of them?
You are correct with your expected & actual understanding. int x=1+2; assertEquals(x,2);
assertEquals() is a method that takes a minimum of 2 arguments and compares actual results with expected results. If both match, then the assertion is passed and the test case is marked as passed.
Selenium doesn't support any kind of the assertion, you have go with the frameworks ex: testNG , JUnit ... I can suggest you 2 methods for asserting multiple values using testNG by assuming you have stored multiple values in ArrayList . You may have to change the logic little based on the data structure you are using.
To compare integer values in Java, we can use either the equals() method or == (equals operator). Both are used to compare two values, but the == operator checks reference equality of two integer objects, whereas the equal() method checks the integer values only (primitive and non-primitive).
Using the Hamcrest CoreMatcher
(included in JUnit 4.4 and later) and assertThat()
:
assertThat(myString, anyOf(is("value1"), is("value2")));
I would use AssertJ for this:
import static org.assertj.core.api.Assertions.*; assertThat("hello").isIn("hello", "world");
It's more concise and it will give you a descriptive message when the assertion fails.
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