Comparing arrays in JUnit assertions, concise built-in way?

157,736

Solution 1

Use org.junit.Assert's method assertArrayEquals:

import org.junit.Assert;
...

Assert.assertArrayEquals( expectedResult, result );

If this method is not available, you may have accidentally imported the Assert class from junit.framework.

Solution 2

You can use Arrays.equals(..):

assertTrue(Arrays.equals(expectedResult, result));

Solution 3

I prefer to convert arrays to strings:

Assert.assertEquals(
                Arrays.toString(values),
                Arrays.toString(new int[] { 7, 8, 9, 3 }));

this way I can see clearly where wrong values are. This works effectively only for small sized arrays, but I rarely use arrays with more items than 7 in my unit tests.

This method works for primitive types and for other types when overload of toString returns all essential information.

Solution 4

Assert.assertArrayEquals("message", expectedResult, result)

Solution 5

JUnit 5 we can just import Assertions and use Assertions.assertArrayEquals method

import org.junit.jupiter.api.Assertions;

Assertions.assertArrayEquals(resultArray,actualResult);
Share:
157,736

Related videos on Youtube

mBria
Author by

mBria

I dig bringing great things into the world, and sharing with other people. I've been doing various forms of bad-ass software for years. These days I am a Technology Director and Principle Consultant for Turnberry Solutions. I write often on my blog there as well as for the well-respected tech publishing site InfoQ. Frankly, I love doing this stuff, and love helping others love it too.

Updated on June 20, 2021

Comments

  • mBria
    mBria almost 3 years

    Is there a concise, built-in way to do equals assertions on two like-typed arrays in JUnit? By default (at least in JUnit 4) it seems to do an instance compare on the array object itself.

    EG, doesn't work:

    int[] expectedResult = new int[] { 116800,  116800 };
    int[] result = new GraphixMask().sortedAreas(rectangles);
    assertEquals(expectedResult, result);
    

    Of course, I can do it manually with:

    assertEquals(expectedResult.length, result.length);
    for (int i = 0; i < expectedResult.length; i++)
        assertEquals("mismatch at " + i, expectedResult[i], result[i]);
    

    ..but is there a better way?

  • mBria
    mBria over 13 years
    What stinks about that though is you get NO data about what went wrong when it fails.
  • mBria
    mBria over 13 years
    Hm, I don't see any 'assertArrayEquals' in my 'junit.framework.Assert'?
  • mBria
    mBria over 13 years
    4.8.1 is what I have, and what appears to be the latest available via Maven (grepcode.com/…). Is it only in 4.8.2 or 4.9?
  • Zitrax
    Zitrax over 10 years
    Nice when you are on an older junit version (like on Android)
  • Erdem
    Erdem over 8 years
    If you want to see which bytes don't match you can convert them to string: assertEquals(Arrays.toString(expectedResult), Arrays.toString(result));
  • user1075613
    user1075613 over 5 years
    but all you get when it fails for different length is java.lang.AssertionError: array lengths differed, expected.length=6 actual.length=7. As most JUnit failure messages it's not so helpful...I advise using some assertion framework
  • Andy Thomas
    Andy Thomas over 5 years
    @user1075613 - I find it helpful. We asserted the arrays were equal, they're not, and we're given an indication why. From there, we can set a breakpoint, and examine the arrays in detail.
  • user1075613
    user1075613 over 5 years
    right, it's - a bit - helpful. However as you point it out, the instant you have this message you ask yourself "why it's not the same length?" so you want to check the content. Why losing time with a debugger when a good error message could tell it directly? (sure you still need the debugger sometimes but most of the time you don't)
  • Andy Thomas
    Andy Thomas over 5 years
    You can submit issues to JUnit's issue tracking system. Bear in mind, though, that 1) failing fast, in O(1), can be an advantage, 2) the assertion failure output should not be O(n). The JUnit issue tracking system is a better forum for further discussion.
  • Snackoverflow
    Snackoverflow almost 4 years
    How do you assert that the arrays are not equal element-by-element?
  • Andy Thomas
    Andy Thomas almost 4 years
    @anddero - Assert.assertFalse( Arrays.equals( expectedResult, result )).
  • Snackoverflow
    Snackoverflow almost 4 years
    @AndyThomas That's not the JUnit way. When comparing two files and the assertion would fail, it would not tell you at what position there is a wrong byte. It would just say "assertion failed".
  • Andy Thomas
    Andy Thomas almost 4 years
    @anddero - I may have misunderstood. Are you asking how to test that every corresponding pair of elements is unequal? That's worth asking as a separate question.
  • Snackoverflow
    Snackoverflow almost 4 years
    @AndyThomas No, I was actually asking for the exact opposite of assertArrayEquals which would be assertArrayNotEquals (it does not exist). But I just realized, when testing for inequality, the meaning of "assertion failed" is enough, because all the elements are equal, so there is no need for a comparison output. So you actually answered my question with Arrays.equals.
  • Hsiao-Ting
    Hsiao-Ting almost 2 years
    I like this solution
  • z32a7ul
    z32a7ul almost 2 years
    Note, however, that assertEquals( Arrays.toString( new String[]{ "artiforg", "blobel", "kipple", "tench" } ), Arrays.toString( new String[]{ "artiforg", "blobel, kipple", "tench" } ) ); will pass. (The first array consists of three strings, the second of four strings.)