title: Testing dao layer with @DataJpaTest
body: <p>I am writing unit testing using <strong>@DataJpaTest</strong>. Though it should do automatic rollback after every method, it is not doing that. Can you please help me with this. </p>
  
  <p>This has 2 test cases written, ideally, test2 should return null, but it returns 1.</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @DataJpaTest
  public class EmployeeRepositoryTest {
  
      @Autowired TestEntityManager em;
  
      @Autowired EmployeeRepository rep;
  
  //  @Autowired EmployeeService service;
      //Spring context loaded only once, reused by other methods
      @Test
      public void test1() {
  //      System.out.println(service); No such bean found
          Employee e= new Employee();
          e.setName("Payal");
          em.persist(e);
          em.flush();
          Employee emp=rep.findByName("Payal");
          assertNotNull(emp);
          assertThat(emp.getId()).isGreaterThan(0);
      }
      @Test
      public void test2() {
          Employee emp=rep.findByName("Payal");
          assertNull(emp);
      }
  }
  </code></pre>
  
  <p>Complete code can be found at: 
  <a href="https://github.com/payalbnsl/SpringUnitTestDemo/tree/master/src/test/java/com/example/demo/dao" rel="nofollow noreferrer">https://github.com/payalbnsl/SpringUnitTestDemo/tree/master/src/test/java/com/example/demo/dao</a></p>