SHARE
TWEET

mongoDB testcontainers JUNIT5

Mitis Jan 21st, 2020 (edited) 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.demomongo;
  2.  
  3. import com.example.demomongo.dao.PersonRepository;
  4. import com.example.demomongo.model.Address;
  5. import com.example.demomongo.model.Person;
  6. import org.junit.jupiter.api.Assertions;
  7. import org.junit.jupiter.api.Test;
  8. import org.junit.jupiter.api.extension.ExtendWith;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import org.springframework.context.ApplicationContextInitializer;
  13. import org.springframework.context.ConfigurableApplicationContext;
  14. import org.springframework.data.mongodb.core.MongoTemplate;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.test.context.ContextConfiguration;
  17. import org.springframework.test.context.junit.jupiter.SpringExtension;
  18. import org.springframework.test.context.support.TestPropertySourceUtils;
  19. import org.springframework.test.web.servlet.MockMvc;
  20. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  21. import org.testcontainers.containers.GenericContainer;
  22. import org.testcontainers.junit.jupiter.Container;
  23. import org.testcontainers.junit.jupiter.Testcontainers;
  24. import java.util.List;
  25. import static java.lang.String.format;
  26. import static org.junit.jupiter.api.Assertions.*;
  27. import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
  28. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  29.  
  30.  
  31. /**
  32. * Jupiter integration is provided by means of the @Testcontainers annotation.
  33. *
  34. * The extension finds all fields that are annotated with @Container and calls their container lifecycle methods (methods on the Startable interface).
  35. * Containers declared as static fields will be shared between test methods. They will be started only once before any test method is executed and
  36. * stopped after the last test method has executed. Containers declared as instance fields will be started and stopped for every test method.
  37. */
  38. @ExtendWith(SpringExtension.class)
  39. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  40. @Testcontainers
  41. @AutoConfigureMockMvc
  42. @ContextConfiguration(initializers = DemomongoDockerJUnit5TestClass.Initializer.class)
  43.  
  44. public class DemomongoDockerJUnit5TestClass {
  45.  
  46.     @Autowired
  47.     private MongoTemplate mongoTemplate;
  48.  
  49.     @Autowired
  50.     PersonRepository personRepository;
  51.  
  52.     @Autowired
  53.     protected MockMvc mvc;
  54.  
  55.     //anotation container that are shared between all methods of a test class
  56.     @Container
  57.     public static GenericContainer mongoContainer = new GenericContainer("mongo:4.0");
  58.  
  59.     //properties for connecting to docker database
  60.     static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
  61.  
  62.         @Override
  63.         public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
  64.  
  65.             TestPropertySourceUtils.addInlinedPropertiesToEnvironment(configurableApplicationContext,
  66.                     format("spring.data.mongodb.uri=mongodb://%s:%s",mongoContainer.getContainerIpAddress(), mongoContainer.getMappedPort(27017)));
  67.         }
  68.     }
  69.  
  70.     @Test
  71.     public void testDatabase(){
  72.  
  73.         mongoTemplate.insert(new Person("Person First",3, "0905567888","emailfirst@email.cz",1000, new Address("Zlin", "Czech republic")));
  74.         mongoTemplate.insert(new Person("Person Second",3, "0905567888","emailfirst@email.cz",1000, new Address("Zlin", "Czech republic")));
  75.         List<Person> all = personRepository.findAll();
  76.  
  77.         //there are 6 entities of person because 4 we already inserted at DatabaseSeeder.class and another two we inserted here
  78.         Assertions.assertEquals(6,all.size());
  79.     }
  80.  
  81.     @Test
  82.     public void testRepository() {
  83.         Person p= new Person("Person First",3, "0905567888","emailfirst@email.cz",1000, new Address("Zlin", "Czech republic"));
  84.         List<Person> books = personRepository.findAll();
  85.         assertNotNull(books);
  86.     }
  87.  
  88.  
  89.     @Test
  90.     public void getAllPersonsAPI() throws Exception
  91.     {
  92.         mvc.perform( MockMvcRequestBuilders
  93.                 .get("/persons/all")
  94.                 .accept(MediaType.APPLICATION_JSON))
  95.                 .andDo(print())
  96.                 .andExpect(status().isOk());
  97.     }
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top