We will write a simple unit test to test this repository. The code for the unit test is shown as follows:
@DataMongoTest @RunWith(SpringRunner.class) public class PersonMongoDbRepositoryTest { @Autowired PersonMongoDbRepository personRepository; @Test public void simpleTest(){ personRepository.deleteAll(); personRepository.save(new Person( "name1")); personRepository.save(new Person( "name2")); for (Person person : personRepository.findAll()) { System.out.println(person); } System.out.println(personRepository.findByName("name1")); System.out.println(personRepository.count()); } }
Some important things to note are as follows:
- Make sure that MongoDB is running when your run the test.
- @DataMongoTest: The DataMongoTest annotation ...