Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit AsserEquals() failing because of extra whitespace on array

Tags:

java

junit

A Junit test of mine is failing when comparing 2 arrays with kafka producer records in them. The failure is one extra space after the array.

Expected:

java.util.ArrayList<[ProducerRecord(topic=producer-test, partition=null, 
headers=RecordHeaders(headers = [], isReadOnly = false), key=0, 
value=Message: test-message,
Number: 0, timestamp=null), ProducerRecord(topic=producer-test, 
partition=null, headers=RecordHeaders(headers = [], isReadOnly = false), 
key=1, value=Message: test-message,
Number: 1, timestamp=null)]> 

Actual:

java.util.ArrayList<[ProducerRecord(topic=producer-test, partition=null, 
headers=RecordHeaders(headers = [], isReadOnly = false), key=0, 
value=Message: test-message,
Number: 0, timestamp=null), ProducerRecord(topic=producer-test, 
partition=null, headers=RecordHeaders(headers = [], isReadOnly = false), 
key=1, value=Message: test-message,
Number: 1, timestamp=null)]>

The IDE tells me that the only difference is one space after the end of the arrayList in Expected, which you can see by highlighting the end. What is going on?!

Edit:

Here is some of the other code

List<ProducerRecord<Integer, TestObj>> history = producer.history();

    //To be inserted into expected
    TestObj obj0 = new TestObj("test-message", 0);
    TestObj obj1 = new TestObj("test-message", 1);

    //new arraylist is needed or else the lists have slightly different types for some reason
    List<ProducerRecord<Integer, TestObj>> expected = new ArrayList<ProducerRecord<Integer, TestObj>>(Arrays.asList(
            new ProducerRecord<Integer, TestObj>("producer-test", 0, obj0),
            new ProducerRecord<Integer, TestObj>("producer-test", 1, obj1)
    ));

    Assert.assertEquals("Sent didn't match expected!", expected, history);
like image 372
kschnied Avatar asked Nov 19 '25 20:11

kschnied


1 Answers

Assert.assertEquals() is invoking expected.equals(history)

The definition for List.equals(Object o) is the following:

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Have you defined an equals method for your ProducerRecord class?

If you have not, then your ProducerRecord objects will be compared by reference equality, meaning that they will only be compared on the basis if they are actually the same objects. Since you are constructing new ProducerRecord objects within your test, they will not have reference equality.

like image 76
Rick Ridley Avatar answered Nov 24 '25 23:11

Rick Ridley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!