Sam Sam - 22 days ago 6
Java Question

junit arrays not equal test

I'm trying to write a test case where my scenario is that two byte arrays should be not equal.

Can I do this with junit?

Or do I have to use something external like Hamcrest? I couldn't change the code in this answer to do the job

Please give a sample.

Thanks in advance.

Answer

I prefer doing this the Hamcrest way, which is more expressive:

Assert.assertThat(array1, IsNot.not(IsEqual.equalTo(array2)));

Or the short version with static imports:

assertThat(array1, not(equalTo(array2)));

(The IsEqual matcher is smart enough to understand arrays, fortunately.)

Note that a limited version of Hamcrest is part of the JUnit 4.x distribution, so you don't need to add an external library.