I just noticed that jUnit 4.8.1 does not include support for testing two boolean arrays for equality. There are tons of other assertArrayEquals but none to take in two boolean arrays.
Is there a proper way to do this? My current thinking is that I'd have to iterate over an array and use something like
Assert.assertTrue(arrayOne[i] == arrayTwo[i]);
Is there a cleaner way to do this?
You can use Arrays.equals()
to compare the two arrays, then assert that they are equivalent.
Assert.assertTrue(Arrays.equals(arrayOne, arrayTwo));
Arrays.equals()
checks the length and each element in the array, so you won't have to worry about iterating over each array.
There is also Assert.assertArrayEquals
, which will give you the exact position that the arrays differed at.
Example: for a test written as such:
@Test
public void doArrayTest() {
int[] foo = {1, 2, 3};
int[] bar = {4, 5, 6};
assertArrayEquals(foo, bar);
}
The result would be:
arrays first differed at element [0]; expected:<1> but was:<4>
Expected :1
Actual :4
The functionality has been added in JUnit 4.12, which was released in Dec. 2014.
assertArrayEquals(boolean[] expecteds, boolean[] actuals)
assertArrayEquals(String message, boolean[] expecteds, boolean[] actuals)
For reference: this the the PR that contains the commit: https://github.com/junit-team/junit/pull/632
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