Unit test

Let's write a simple unit test to test whether we are able to access the todo data using TodoRepository. The following snippet shows the important details:

    @DataJpaTest    @RunWith(SpringRunner.class)    public class TodoRepositoryTest {      @Autowired      TodoRepository todoRepository;      @Test      public void check_todo_count() {        assertEquals(3, todoRepository.count());      }    }

A few important things to note are as follows:

  • @DataJpaTest: The DataJpaTest annotation is typically used along with SpringRunner in JPA repository unit tests. This annotation will enable only JPA-related auto-configuration. The test would use an in-memory database by default.
  • @RunWith(SpringRunner.class): SpringRunner is a simple alias for SpringJUnit4ClassRunner. It launches ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.