See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

how to write Junit test case for spring boot rest controller which uses mongodb for data persist

General Tech Bugs & Fixes
Max. 2000 characters

Priya Roy


( 8 months ago )


I am trying to write junit test cases for my spring boot controller.

I have two API getUser and AddUser, these APIs talk to mongotemplate over dao to put the data in mongo DB and get the data from mongo DB.

I have MongoConfiguration class(it has server address, port password etc) is configured @Value by property file which is configured through @PropertySource

mongo template is created with details reading from @Autowired mongoconfig class.

when I write junit test case and tested, it was using actual mongoDB which was configured for all developers through standard properties file, so for my testing I want to use some kind of embedded mongo to mock the mongo db template

So how to avoid using my actual mongodb during my junit test cases

This is my below code which is working with actual db

public class UserControllerTest extends AbstractTest {
   public void setUp() {
   public void createProduct() throws Exception {
       User user =  new User();

      String inputJson = super.mapToJson(user);
      String uri = "/iot/user/createuser";
      MvcResult mvcResult = mvc.perform(

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


what's your interest