indra
indra

Reputation: 832

Junit for spring boot restcontroller which uses mongodb

I have restcontroller which is uses internally uses mongodb to persisit data , now I want to write junit test-cases which should not use the actual DB or collection of my backend mongoDB. I want to use similar to H2 test database.

I have written below code but dont know how to avoid writing in to mongo my original db.

public class APiControllerTest extends AbstractTest {
   @Override
   @Before
   public void setUp() {
      super.setUp();
   }
   @Test
   public void AddUser throws Exception {
       User user =  new User();
       user.setName("junit");

      String inputJson = super.mapToJson(user);
      String uri = "/v1/createuser";
      MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri)
         .contentType(MediaType.APPLICATION_JSON_VALUE)
         .content(inputJson)).andReturn();

      int status = mvcResult.getResponse().getStatus();
      assertEquals(201, status);

   }
}  

Upvotes: 1

Views: 55

Answers (0)

Related Questions