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 ...