Unit test

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

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.