title: Spring Hibernate Repository Testing with jUnit
body: <p>First of all I am a newcomer for Spring + Hibernate development. Followed lots of tutorials books and I have created a sample application mainly on Spring, Hibernate based on the standards, I have started writing some test cases for Repository (DAO) methods ie find, findAll, save, delete.</p>
  
  <p>Its really crazy when i execute the test class not all of the test cases execute properly for ex. particularly find &amp; findAll methods. But when run them individually they pass perfectly.</p>
  
  <h2>Entity</h2>
  
  <pre><code>@Entity
  @Table(name = "client_master")
  public class ClientMaster {
  
  private Long id;
  private Long version;
  private Date dateCreated;
  private Date lastUpdated;
  private String clientName;
  
  private List&lt;ProjectMaster&gt; projectMaster;
  
  @Id
  @GeneratedValue
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  @Version
  @Column(name = "version")
  public Long getVersion() {
      return version;
  }
  
  public void setVersion(Long version) {
      this.version = version;
  }
  
  @Column(name = "client_name", nullable= false,unique= true)
  public String getClientName() {
      return clientName;
  }
  
  public void setClientName(String clientName) {
      this.clientName = clientName;
  }
  
  @OneToMany(targetEntity = ProjectMaster.class, mappedBy = "lientMaster", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  public List&lt;ProjectMaster&gt; getProjectMaster() {
      return projectMaster;
  }
  
  public void setProjectMaster(List&lt;ProjectMaster&gt; ProjectMaster) {
      this.projectMaster = projectMaster;
  }
  
  @Column(name = "date_created" , nullable= false)
  @Temporal(TemporalType.TIMESTAMP)
  public Date getDateCreated() {
      return dateCreated;
  }
  
  public void setDateCreated(Date dateCreated) {
      this.dateCreated = dateCreated;
  }
  
  @Column(name = "last_updated")
  @Temporal(TemporalType.TIMESTAMP)
  public Date getLastUpdated() {
      return lastUpdated;
  }
  
  public void setLastUpdated(Date lastUpdated) {
      this.lastUpdated = lastUpdated;
  }
  
  }  
  </code></pre>
  
  <h2>Repository</h2>
  
  <pre><code>import java.util.List;
  import javax.persistence.EntityNotFoundException;
  import org.hibernate.Query;
  
  @Repository("clientMasterRepo")
  @Transactional
  public class ClientMasterRepoHibernate implements ClientMasterRepository {
  
  @Autowired
  private SessionFactory sessionFactory;
  
  @Override
  public ClientMaster find(Long id) {     
      // Based on the Hibernate currentsession get the ClientMaster Object based on Id        
      ClientMaster clientMaster = (ClientMaster) sessionFactory.getCurrentSession().get(ClientMaster.class, id);              
      return clientMaster;
  }
  
  @Override
  public List&lt;ClientMaster&gt; findAll() {
      // Get all the ClientMaster records.
      Query query = sessionFactory.getCurrentSession().createQuery("FROM ClientMaster");
      List&lt;ClientMaster&gt; clientMasterList = query.list();
      return clientMasterList;
  
  }
  
  @Override
  public ClientMaster save(ClientMaster clientMaster) {
      // Insert or Update the ClientMaster object
      sessionFactory.getCurrentSession().saveOrUpdate(clientMaster);
      return CclientMaster;       
  }
  
  @Override
  public void delete(ClientMaster clientMaster) {
      // Delete the ClientMaster object
      sessionFactory.getCurrentSession().delete(clientMaster);
  }
  
  }
  </code></pre>
  
  <h2>JUnit Tests</h2>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"file:src/main/resources/META-INF/spring/spring-master.xml"})  
  public class ClientMasterRepositoryTests {
  
  @Autowired
  private ClientMasterRepository clientMasterRepository;
  
  private List&lt;ClientMaster&gt; ClientMasterList;
  
  @Before
  public void setup(){
      // Initialize ClientMaster mock objects for testing     
      ClientMasterList = new ArrayList&lt;ClientMaster&gt;();
  
      ClientMaster client1 = new ClientMaster();
      client1.setClientName("Client1");
      client1.setDateCreated(new Date());
      client1.setLastUpdated(new Date());
  
      ClientMaster client2 = new ClientMaster();
      client2.setClientName("Client2");
      client2.setDateCreated(new Date());
      client2.setLastUpdated(new Date());
  
      ClientMaster client3 = new ClientMaster();
      client3.setClientName("Client3");
      client3.setDateCreated(new Date());
      client3.setLastUpdated(new Date());
  
      ClientMaster client4 = new ClientMaster();
      client4.setClientName("Client4");
      client4.setDateCreated(new Date());
      client4.setLastUpdated(new Date());
  
      ClientMasterList.add(client1);
      ClientMasterList.add(client2);
      ClientMasterList.add(client3);
      ClientMasterList.add(client4);      
  }
  
  @Test
  public void testSave(){     
      for (Iterator iterator = ClientMasterList.iterator(); iterator.hasNext();) {
          ClientMaster ClientMaster = (ClientMaster) iterator.next(); 
  
          // insert the ClientMaster object 
          clientMasterRepository.save(ClientMaster);
  
          assertTrue(ClientMaster.getClientName()+" is saved - Id "+ClientMaster.getId(),ClientMaster.getId() &gt; 0);
      }       
  }
  
  @Test
  public void testUpdate(){
      // get a ClientMaster object from the repository
      ClientMaster clientMasterObj = clientMasterRepository.find(1L);
  
      // assert if its not null
      assertTrue(!clientMasterObj.getClientName().isEmpty());
  
      // change the client name 
      clientMasterObj.setClientName("Client1-Changed");
  
      // update the ClientMaster object
      clientMasterRepository.save(clientMasterObj);
  
      // assert the value changed id true
      assertEquals("Client1-Changed", clientMasterRepository.find(1L).getClientName());
  }
  
  @Test   
  public void testDelete(){
      // get a CASClientMaster object from the repository
      ClientMaster clientMasterObjBeforeDel = clientMasterRepository.find(2L);
  
      // delete the ClientMaster object
      clientMasterRepository.delete(clientMasterObjBeforeDel);
  
      // get a ClientMaster object from the repository
      ClientMaster clientMasterObjAfterDel = clientMasterRepository.find(2L);
  
      // get the ClientMaster object ID 
      assertNull(clientMasterObjAfterDel);
  }
  
  @Test
  public void testFind(){
  
      // get a ClientMaster object from the repository
      ClientMaster clientMasterObj = clientMasterRepository.find(3L);             
  
      // compare the id's of passed and retrieved objects.
      assertThat(clientMasterObj.getId(), is(3L));
      assertTrue(clientMasterObj.getId() == 3);       
  }
  
  @Test
  public void testFindAll(){
      // get all ClientMaster object from the repository 
      List&lt;ClientMaster&gt; ClientMasterList = clientMasterRepository.findAll();             
  
      // check if it returns all records from DB
      assertTrue(ClientMasterList.size() &gt; 0);
      assertThat(ClientMasterList.size(), is(4));
  }
  
  }
  </code></pre>
  
  <p>When I execute above all test cases entirely, find &amp; findAll test cases will be failed but pass when those executed individually. I am a newbie to testing framework. Let me know if there is anything wrong with above methodology to test the repository layer.</p>
  
  <h1>UPDATE</h1>
  
  <p>Even update test case is behaving strangely for some execution works properly in some it gives error as "NullPointerException" when i fetch the clientMaster object with find method.</p>
  
  <p><img src="https://i.stack.imgur.com/jXlwe.png" alt="enter image description here"></p>