How to Test Spring MVC along with repository MongoDb

Can someone please help me with junit testing of service, controller and repository. I’m getting lots of error when I write test cases for service and controller class.

This is my service class

import com.controller.ValidatorClass;
import com.model.Entity;
import com.model.Status;
import com.repository.Repository;

public class Service {

    private Repository repository;
    private SequenceGeneratorService service;
    public ResponseEntity storeInDb(ExecutorEntity model) {
        ValidatorClass validation = new ValidatorClass();
        Map<String, String> objValidate = validation.getInput(model.getLink(), model.getUsername(), 
                model.getPassword(), model.getSolution());
        return new ResponseEntity(model, HttpStatus.OK);
    public List<String> handleValidationExceptions(MethodArgumentNotValidException ex) {
        return ex.getBindingResult()

This is my controller class

@RequestMapping(value = "/create")
public class Controller {

    private Service service;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseEntity code(@Valid @RequestBody Entity model) {
        return service.storeInDb(model);

My model class

    public static final String SEQUENCE_NAME = "user_sequence";

    @NotNull(message = "Name can not be Null")
    private String username;

    @NotNull(message = "Password can not be Null")
    private String password;

    @NotNull(message = "Jenkins Link can not be Null")
    private String Link;

    @NotNull(message = "Solution can not be Null")
    private String solution;

    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private Status status; //enum class having READY and FAIL as values.

along with getters and setters.


JUnit is an extensive topic that you should read you up on:

Please note that JUnit 5 is current version (I see that you’ve tagged your question with junit4).

I’ll give you an idea on how to write some integration tests just to get you started. You will probably come across TestRestTemplate during your journey to wisdom, but it is now recommended to use WebTestClient.

Tests below will bring all parts of your application up-and-running. When you’re getting more experienced, you will probably test slices of your application as well.

package no.yourcompany.yourapp.somepackage;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ExecutorControllerTest {

    WebTestClient webTestClient;

    public void postExecutor_validModel_receiveOk() {

    public void postExecutor_validModel_receiveResponseEntity() {
                .value(responseEntity -> assertThat(responseEntity).isNotNull());

    private static ExecutorEntity createValidExecutorEntity() {

With dependency to spring-boot-starter-test, it is not necessary to add explicit dependencies to JUnit.


In order to use WebTestClient, add the following to your POM:


Leave a Reply

Your email address will not be published. Required fields are marked *