JUnit "equals" comparison failed

org.junit.ComparisonFailure
Javadoc
Thrown when an {@link org.junit.Assert#assertEquals(Object, Object) assertEquals(String, String)} fails. Create and throw a ComparisonFailure manually if you want to show users the difference between two complex strings.

Inspired by a patch from Alex Chaffee (alex@purpletech.com) @since 4.0
at org.junit.Assert.assertEquals
(source)

Typical Exception Messages

  1. <No message>
  2. expected:<[]OVERRIDDEN> but was:<[NON ]OVERRIDDEN>
  3. expected:<[1]> but was:<[0]>
  4. expected:<[success]> but was:<[failed]>
  5. expected:<<memory:[8192, vCores:8]>> but was:<<memory:[4096, vCores:4]>>
  6. expected:<[NON ]OVERRIDDEN> but was:<[]OVERRIDDEN>
  7. expected:<[h2]> but was:<[undefined]>
  8. expected:<[undefined]> but was:<[Test-Product]>
  9. expected:<[localhost]:12345> but was:<[127.0.0.1]:12345>
  10. expected:<gradle is gr[8]> but was:<gradle is gr[eat]>

Specific cases

Solution

igorski, 2 months ago

ComparisonFailure is thrown when using JUnit's internal assertions to compare two strings, number or objects that are not equal. The simplest code to reproduce the error is the following:

import static org.junit.Assert.assertEquals;

public class ObjectComparisonTest { 

   @Test 
   public void shouldAssertStrings() { 
      assertEquals("Jhon", "John");  //The letters are in different order
   } 

   @Test 
   public void shouldAssertNumbers() { 
      assertEquals(1, 2); 
   } 

   @Test 
   public void shouldAssertObjects() {
      Person p1 = new Person();
      Person p2 = new Person();
      assertEquals(p1, p2);  // The object instances are not the same
   } 
}

class Person { ... }

Mind the static import. The assertEquals is part of the JUnit API.

External results for this pattern (657)

  1. tcurdtvia GitHub2 days ago
    unexpected resolve for #!/usr/bin/python3 -B line = line.replace('@ALLOW_BATCH_FILTER@', config[self.client]['ALLOW_BATCH_FILTER']) expected:<..., config[self.client[]['ALLOW_BATCH_FILTER'])]> but was:<..., config[self.client[[]'ALLOW_BATCH_FILTER']) ]>
    Show stack trace
  2. mcomellavia GitHub3 days ago
    for input:a网络A.网络.cn|| expected:<[网络.]cn> but was:<[]cn>
    Show stack trace
  3. julianofischervia GitHub1 week ago
    group_size.ccs.report differs! expected:<... 347 2 8 ... 1794...>
    Show stack trace
  4. chandramsvia GitHub1 week ago
    Record has incorrect formatted message expected:&lt;[debug message]&gt; but was:&lt;[TRAS3004E: Failed to write messages to the <user home>/git/open-liberty/dev/com.ibm.ws.logging.hpel/../com.ibm.ws.logging.hpel/build/unittest-logs/messages.log file.]&gt;
    Show stack trace
  5. ruckcvia GitHub1 week ago
    expected:<[authenticationFailure]> but was:<[error]>
    Show stack trace
  6. mjmeansvia GitHub1 week ago
    Errors encountered while loading Pathfinder RPG for Players expected:<[]> but was:<[Unconstructed Reference: PCTemplate Default Language ~ Tengu, Unconstructed Reference: PCTemplate Tiefling (Base)]>
    Show stack trace
  7. vmassolvia GitHub1 week ago
    expected:<...s org.xwiki.context.[ExecutionContextPropertyTest$SomeSubClass], but was [class org.xwiki.context.]ExecutionContextProp...> but was:<...s org.xwiki.context.[AmplExecutionContextPropertyTest$SomeSubClass], but was [class org.xwiki.context.Ampl]ExecutionContextProp...>
    Show stack trace
  8. nanojovervia GitHub1 week ago
    expected:<Elegir Servicio[]> but was:<Elegir Servicio[1]> --
    Show stack trace
  9. seenureddyvia GitHub2 weeks ago
    expected:<[06d1165a-9352-489f-b537-132a229e725d]> but was:<[93f13344-a0b4-476c-acde-80d87e7e4737]>
    Show stack trace
  10. a1exshvia GitHub3 weeks ago
    expected:<http://example.org/[]event-types/one> but was:<http://example.org/[/]event-types/one>
    Show stack trace