title: JUnit, assertEquals false when two objects seem exactly the same
body: <p>I'm testing a Spring controller and I've mocked the loginService which I call in it.</p>
  <p>I got errors in Unit Testing on the response of the controller but, as I can see in debug mode, the two elements compared seem exactly the same.</p>
  <p>Below you can see the image of the debugger that shows that the two elements are equals.</p>
  <p>Now let's see the code below.</p>
  <p>System Under Test (RestController.class)</p>
  <pre><code>@org.springframework.web.bind.annotation.RestController
  public class RestController {
  
  @Autowired
  LoginService loginService;  
  ...
  //METHOD BEING TESTED
  @RequestMapping(value = &quot;/login&quot;, method = POST)
  public ResponseEntity&lt;JsonResponseBody&gt; loginUser(@RequestParam(value =&quot;id&quot;) String id, @RequestParam(value=&quot;password&quot;) String pwd){
      try {
          Optional&lt;User&gt; userr = loginService.getUserFromDbAndVerifyPassword(id, pwd);      //verify the presence into the database
          if (userr.isPresent()) {
              User user = userr.get();                                  //get the User from the optional got from the DB
              String jwt = loginService.createJwt(user.getId(), user.getUsername(), user.getPermission(), new Date());
              //set the jwt token into the header of response
              return ResponseEntity.status(HttpStatus.OK).header(&quot;jwt&quot;, jwt).body(new JsonResponseBody(HttpStatus.OK.value(),&quot;Success! User logged in.&quot; + jwt));
          }
      }catch(UserNotLoggedException e1){ //thrown by loginService#getUserFromDbAndVerifyPassword
          return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new JsonResponseBody(HttpStatus.FORBIDDEN.value(),&quot;Login failed! Wrong credentials. &quot; + e1.toString()));
      }catch(UnsupportedEncodingException e2){  //thrown by loginService#createJwt
         return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new JsonResponseBody(HttpStatus.FORBIDDEN.value(),&quot;Login failed! Encoding permission token error. &quot; + e2.toString()));
      }
      //send response to client
      return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new JsonResponseBody(HttpStatus.FORBIDDEN.value(),&quot;Login failed! No corrispondence found into the database of users.&quot;));
  }
  
  ...
  //INNER CLASS USING LOMBOK
  @AllArgsConstructor
  public class JsonResponseBody{
      @Getter @Setter
      private int server;
      @Getter @Setter
      private Object response;
  }
  ...
  }
  </code></pre>
  <p>Test class:</p>
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class RestControllerTest {
  
     @InjectMocks
     RestController restController;
  
     @Mock
     LoginService loginService;
  
     @Test
     public void loginUserWithSuccessTest() throws UserNotLoggedException, UnsupportedEncodingException{
         User user = new User(&quot;BDAGPP32E08F205K&quot;, &quot;Pippo Baudo&quot;, &quot;ILoveSanRemoEncrypted&quot;, &quot;conduttore&quot;);
         Optional&lt;User&gt; fakeUserOptional = Optional.of(user);
         when(loginService.getUserFromDbAndVerifyPassword(&quot;BDAGPP32E08F205K&quot;,&quot;ILoveSanRemo&quot;)).thenReturn(fakeUserOptional);
  
         String jwt = &quot;aaaa.bbbb.cccc&quot;;
         when(loginService.createJwt(eq(user.getId()), eq(user.getUsername()), eq(user.getPermission()), any(Date.class))).thenReturn(jwt);
  
         ResponseEntity serverResponse = restController.loginUser(&quot;BDAGPP32E08F205K&quot;,&quot;ILoveSanRemo&quot;);
         RestController.JsonResponseBody responseBody = restController.new JsonResponseBody(HttpStatus.OK.value(), &quot;Success! User logged in.&quot; + jwt);
  
         assertEquals(serverResponse.getStatusCode(), HttpStatus.OK); //TRUE
  
         boolean areTheyEquals=false;
         if((RestController.JsonResponseBody) serverResponse.getBody() == responseBody){
             areTheyEquals = true;
         }
         assertEquals(areTheyEquals, true); //FALSE?!?!?!?!?
         assertThat((RestController.JsonResponseBody) serverResponse.getBody(), is(responseBody)); //FALSE?!?!?!
      }
  }
  </code></pre>
  <p>The last two asserts result false. When I debug this test I discover how the mock seems to be correct and also, before the asserts the two objects being compared seem absolutely equals.
  The error given by JUnit is this:</p>
  <blockquote>
  <p>java.lang.AssertionError: Expected: is
  com.example.bytecode.SpringBootJWT.controllers.RestController$JsonResponseBody@587c290d</p>
  <p>but: was
  com.example.bytecode.SpringBootJWT.controllers.RestController$JsonResponseBody@4516af24</p>
  </blockquote>
  <p>Below you can see the image of the debugger.</p>
  <p><a href="https://i.stack.imgur.com/bEEXP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bEEXP.png" alt="The Debugger Tab shows how the two elements being compared (serverResponse.body and responseBody) seem exactly equal. But the last two asserts are false" /></a></p>