[Solved] “org.opentest4j.AssertionFailedError: expected:” issue with JUnit test array (workaround to be discussed)

JUnit test array

ps: some random notes of Xiaobai

Directory

  • JUnit test array
    • Problem Description
    • problem analysis
    • Solution

Problem description

Wrote a simple sort, and a simple test sort. There is nothing wrong with the sorting program itself, so I won’t put the code here. Here is the JUnit code:

 void testSort() {<!-- -->
        Sort Sort = new Sort();
        int[] Array1 = {<!-- -->1, 34, 27, 3, 90};
        int[] Array2 = {<!-- -->1, 3, 27, 34, 90};

        assertEquals(Array2, Sort.beginSort(Array1));
    }

Logically, it should pass the test, but the output is indeed wrong:

org.opentest4j.AssertionFailedError: expected: [I@1dac5ef<[1, 3, 27, 34, 90]> but was: [I@5c90e579<[1, 3, 27, 34, 90]>
Expected :[I@1dac5ef
Actual :[I@5c90e579

Problem Analysis

Obviously, the string of “garbled characters” in front of the array caused the test to fail. Analysis found that this string of “garbled characters” also appears when printf int[] does not use toString() (although the content is different), and the data type I am testing now is also int[]. So I Baidu the alarm “Implicit call to ‘toString()’ on array ‘Array’” when there is no toString(), and found that the previous “garbled” is actually the address of the array< /strong>.

Solution

Currently I add toString() when JUnit uses two int[] types and the test passes. But I think this method is not very good.

syntaxbug.com © 2021 All Rights Reserved.