47

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.

Saurabh
  • 71,488
  • 40
  • 181
  • 244
Sam
  • 475
  • 1
  • 4
  • 6

6 Answers6

56

You can use

assertFalse(Arrays.equals(array1, array2));

If you wanted to check they were equal, I would use the following instead.

assertEquals(Arrays.toString(array1), Arrays.toString(array2));

as this produces a readable output as to what was different rather than just failing.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
53

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.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
16

Newer versions of JUnit offer org.junit.Assert.assertArrayEquals(byte[], byte[]), with overloads for other array types. Failures show the first index with a non-match and the differing elements at that index.

I also enjoy assertEquals(Arrays.asList(expected), Arrays.asList(actual)). The Hamcrest-powered rendition mentioned above is probably best.

pholser
  • 4,908
  • 1
  • 27
  • 37
  • The question was to test array NOT equals, right? What about this case without using Hamcrest? Is there a way to do it? – Felipe Mosso Oct 26 '20 at 14:11
3

Here is a possible alternative, which has the advantage of using the same code as assertArrayEquals() :

private void assertArrayNotEquals(byte[] expecteds, byte[] actuals) {
    try {
        assertArrayEquals(expecteds, actuals);
    } catch (AssertionError e) {
        return;
    }
    fail("The arrays are equal");
}
Manur
  • 8,436
  • 2
  • 27
  • 29
  • 2
    It's a nice idea, but this answer doesn't compile as-is, and if it's usable, it's not obvious how. Catching and ignoring an `AssertionError` from the try-catch (to treat it as success) will also catch the assertion error from `fail`, treating it as success too. Suggest updating the code or deleting this answer. – Krease Apr 17 '17 at 23:39
  • 1
    @Krease You are right, I don't know what I was thinking when I posted that. I've corrected the code. – Manur Apr 19 '17 at 21:16
-1

Sorry this is a bit long but it's easy to debug with and you can cut and paste it into your unit test.

private int span = 10;

private boolean equal(byte[] expected, byte[] got) {
    final boolean result;

    String message = null;
    int offset = -1;
    int length = -1;
    if(expected == null && got == null) {
        result = true;
    } else if(expected == null || got == null) {
        message = "One array is null: " + (expected == null ? "expected" : "got");
        result = false;
    } else if(expected.length != got.length) {
        message = "Lengths differ: expected = " + expected.length + ", got = " + got.length;
        result = false;
    } else {
        length = expected.length;
        for(int i = 0; i < length; i++) {
            if(expected[i] != got[i]) {
                offset = i;
                break;
            }
        }
        result = offset == -1;
        if(!result) {
            message = "Contents differ";
        }
    }

    if(!result) {
        System.err.println(message);
        if(offset >= 0) {
            hexDump("Expected: ", expected, offset, length);
            hexDump("     Got: ", got, offset, length);
        }
    }
    return result;
}

private void hexDump(String label, byte[] ba, int offset, int length) {
    System.err.print(label);
    if(ba == null) {
        System.err.println("<null>");
    } else if(ba.length == 0) {
        System.err.println("<zero-length-array>");
    } else {
        // <span> bytes either side

        final int from = Math.max(0, offset - span);
        final int to = Math.min(length, offset + span);
        if(from != 0) {
            System.err.print("(offset:" + from + ") ");
        }
        for(int i = from; i < to; i++) {
            System.err.printf("%02X ", new Byte(ba[i]));
        }
        System.err.println();
    }
}

@Test
public void testExample() {
    assertTrue(equal(new byte[] { 1, 2, 3 }, new byte[] { 1, 8, 3 }));
}
-1

You could do it like this:

assertNotEquals(arrayOne, arrayTwo)
Jimmy ALejandro
  • 434
  • 5
  • 11
  • Sorry but this is just plain wrong. Because assertNotEquals only looks add the referenced object an expression such as : "assertNotEquals(new int[] {42},new int[] {42});" is true. Which goes against the behaviour we expect from assertArrayEquals and therefor we the behaviour we expect from its complement. – Jip Helsen Jun 10 '23 at 20:57