title: JUnit test always fails when comparing two objects
body: <p>I have a Test Class like this,
  basically these was copy-paste from spring-data-examples.</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(classes = CassandraConfig.class)
  public class UserRepositoryTest {
  
  
      @Autowired UserRepository repository;
      @Autowired Session session;
      User user;
  
      @Before
      public void setUp() {
  
          user = new User();
          user.setId(42L);
          user.setUsername("foobar");
          user.setFirstname("firstname");
          user.setLastname("lastname");
      }
  
      /**
       * Saving an object using the Cassandra Repository will create a persistent representation of the object in Cassandra.
       */
      @Test
      public void findSavedUserById() {
  
          user = repository.save(user);
  
          assertThat(repository.findOne(user.getId()), is(user));
  
      }
  
      /**
       * Cassandra can be queries by using query methods annotated with {@link @Query}.
       */
      @Test
      public void findByAnnotatedQueryMethod() {
  
          repository.save(user);
  
          assertThat(repository.findUserByIdIn(1000L), is(nullValue()));
          assertThat(repository.findUserByIdIn(42L), is(equalTo(user)));
      }
  
  }
  </code></pre>
  
  <p>and everytime I tried to run Unit Test,
  I always get these error messages:</p>
  
  <pre><code>20:02:16.672 [DEBUG] [TestEventLogger] com.kashu.demo.UserRepositoryTest &gt; findSavedUserById STARTED
  20:02:16.867 [DEBUG] [TestEventLogger]
  20:02:16.868 [DEBUG] [TestEventLogger] com.kashu.demo.UserRepositoryTest &gt; findSavedUserById FAILED
  20:02:16.868 [DEBUG] [TestEventLogger]     java.lang.AssertionError: expected:&lt;com.kashu.demo.User@554d7139&gt; but was:&lt;com.kashu.demo.User@6643364a&gt;
  20:02:16.869 [DEBUG] [TestEventLogger]         at org.junit.Assert.fail(Assert.java:88)
  20:02:16.869 [QUIET] [system.out] 20:02:16.868 [
  20:02:16.869 [DEBUG] [TestEventLogger]         at org.junit.Assert.failNotEquals(Assert.java:834) 
  20:02:16.869 [QUIET] [system.out] INFO] [org.gradle.api.internal.tasks.testing.worker.TestWorker] Gradle Test Executor 1 finished executing tests.
  20:02:16.870 [DEBUG] [TestEventLogger]         at org.junit.Assert.assertEquals(Assert.java:118)
  20:02:16.870 [DEBUG] [TestEventLogger]         at org.junit.Assert.assertEquals(Assert.java:144)
  20:02:16.870 [DEBUG] [TestEventLogger]         at com.kashu.demo.UserRepositoryTest.findSavedUserById(UserRepositoryTest.java:67)
  20:02:16.871 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  20:02:16.871 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  20:02:16.872 [DEBUG] [TestEventLogger]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  20:02:16.872 [DEBUG] [TestEventLogger]         at java.lang.reflect.Method.invoke(Method.java:498)
  20:02:16.872 [DEBUG] [TestEventLogger]         at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  </code></pre>
  
  <p>it seems like assertThat complaining 2 User objects to be compared are not equal, so should I do something in these code for successful test? if I need to compare equality between 2 objects, is the test code listed here a correct solution?</p>
  
  <hr>
  
  <p><strong>[update]</strong></p>
  
  <p>I add a equals() function in my User entity class, like this:</p>
  
  <pre><code>@Table(value = "users")
  public class User {
  
      @PrimaryKey("user_id") private Long id;
  
      @Column("uname") private String username;
      @Column("fname") private String firstname;
      @Column("lname") private String lastname;
  
      public User(){
  
      }
  
      public User(Long id,String username,String firstname,String lastname) {
          this.id = id;
          this.username = username;
          this.firstname = firstname;
          this.lastname = lastname;
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getFirstname() {
          return firstname;
      }
  
      public void setFirstname(String firstname) {
          this.firstname = firstname;
      }
  
      public String getLastname() {
          return lastname;
      }
  
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
  
      public boolean equals(Object obj){
          if (this==obj) return true;
          if (this.getClass() != obj.getClass()) return false;
          User user = (User) obj ;
             return (this.id == user.getId()) &amp;&amp; 
                     (this.username.equals(user.getUsername())) &amp;&amp;
                     (this.firstname.equals(user.getFirstname())) &amp;&amp; 
                     (this.lastname.equals(user.getLastname()));
      }
  
  
  
  }
  </code></pre>
  
  <p>and tried to run unit test again , but I still got:</p>
  
  <pre><code>20:51:54.037 [DEBUG] [TestEventLogger] com.kashu.demo.UserRepositoryTest &gt; findSavedUserById STARTED
  20:51:54.189 [DEBUG] [TestEventLogger]
  20:51:54.189 [DEBUG] [TestEventLogger] com.kashu.demo.UserRepositoryTest &gt; findSavedUserById FAILED
  20:51:54.189 [DEBUG] [TestEventLogger]     java.lang.AssertionError:
  20:51:54.190 [DEBUG] [TestEventLogger]     Expected: is &lt;com.kashu.demo.User@2fa98d0f&gt;
  20:51:54.190 [DEBUG] [TestEventLogger]          but: was &lt;com.kashu.demo.User@44dab940&gt;
  20:51:54.190 [DEBUG] [TestEventLogger]         at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
  </code></pre>
  
  <p>any suggestions?</p>
  
  <hr>
  
  <p><strong>[update2]</strong></p>
  
  <p>I think maybe the logic I wrote into equals() function was wrong, so I tried fix my User's equals() function to this:</p>
  
  <pre><code>public boolean equals(Object obj) {
      return true;
  }
  </code></pre>
  
  <p>It looks very stupid but I can ensure what's going on, 
  and after I done this and build my project again,
  I saw message:
  Gradle Test Run :test PASSED
  so the problem is actually in equals() function
  Ya , thank you everybody , You help me a lot!
  sorry for my poor English skill :D</p>