Open In App

Spring Boot MockMVC Example

Last Updated : 11 Oct, 2022
Like Article

Automated testing plays a vital role in the software industry. In this article, let us see how to do the testing using MockMvc for a Spring Boot project. To test the web layer, we need MockMvc and by using @AutoConfigureMockMvc, we can write tests that will get injected. SpringBootApplication is an excellent one that adds the following

  1. @Configuration
  2. @EnableAutoConfiguration
  3. @EnableWebMvc
  4. @ComponentScan

The application can ordinarily run as a Java application and hence development wise it is easier. Let’s see the concept via a sample project

Example Project

Project Structure:


As it is a maven project, all dependencies are available under 


Dependencies like

  1. JDK 1.8
  2. Spring version from 2.2.5 onwards
  3. Maven 3.2+ and in the case of Gradle, it is Gradle 4+

We should add all the dependencies in pom.xml(in the case of the Maven project)


<?xml version="1.0" encoding="UTF-8"?>
        <!-- Mandatory -->

We are using MockMvc to test the web layer. That is we should have a model. a controller, and a view. Let us see the controller file

Each and every method should have either Getmapping or Postmapping


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
public class WelcomeMvcController {
    private WelcomeService welcomeService;
    @GetMapping(value = "/")
    public String greeting1(String name, Model model) {
        model.addAttribute("welcome", welcomeService.greetingMessage1(name));
        return "welcome-page";
    @GetMapping(value = "/event")
    public String greeting2(String name, Model model) {
        model.addAttribute("welcomeToEvent", welcomeService.greetingMessage2(name));
        return "event-page";

Required services are written in the service file


import org.springframework.stereotype.Service;
public class WelcomeService {
    public String greetingMessage1(String name) {
        return String.format("Welcome , %s to the world of programming!!!", name );
    public String greetingMessage2(String attendee) {
        return String.format("Welldone , %s You are selected to the contest!!!", attendee );

SpringBoot Application is run as an ordinary Java application only


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class WebAppMain {
    public static void main(String[] args) {, args);

Now let us start to write the test class that tests the web layer by using MockMvc


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
public class WelcomeWebAppTest {
    private MockMvc mockMvc;
    // We have to write out expectations and the
      // expectations need to match with actuals
      // When this is run, it imitates and accesses
      // the web layer and get the output.
    public void testWelcome() throws Exception {
        this.mockMvc.perform(MockMvcRequestBuilders.get("/").param("name", "Geeks"))
                        "Welcome , Geeks to the world of programming!!!"))
    public void testWelcomeToEvent() throws Exception {
        this.mockMvc.perform(MockMvcRequestBuilders.get("/event").param("name", "Geeks"))
                        "Welldone , Geeks You are selected to the contest!!!"))

Once the project is complete and does not contain any errors, we can run the test file as an ordinary JUNIT application


Console Output:

Console Output


Console Output


Once we are getting response 200 means the service is available and the parameters are passed properly and it is producing a positive response. With that response, we are comparing the details by means of


this.mockMvc.perform(MockMvcRequestBuilders.get("/").param("name", "Geeks"))
                .andExpect(MockMvcResultMatchers.status().isOk()) // checking status
                        "Welcome , Geeks to the world of programming!!!")) // sent attribute and its value check
                .andExpect(MockMvcResultMatchers.view().name("welcome-page")) // its view name check

Like this, we can test the web layer in the above-said ways


Automated testing helps to avoid tons of errors and error-free code and helps for good quality software. In the Spring Boot project, we can use MockMvc and can achieve the same.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads