Back to Repositories

Testing REST Controller Endpoints with MockMvc in spring-boot-examples

This test suite demonstrates Spring Boot REST controller testing using MockMvc for the HelloWorldController. It validates HTTP endpoint responses and request handling in a standalone test environment without requiring a full application context.

Test Coverage Overview

The test suite focuses on validating the ‘/hello’ endpoint functionality in isolation.

  • Verifies HTTP GET request handling
  • Validates successful response status codes
  • Tests JSON content type acceptance
  • Ensures proper endpoint configuration

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests without deploying a full server. The standalone setup pattern isolates the controller under test, enabling focused unit testing of web layer components.

Key implementation features include MockMvc request builders, response matchers, and result handlers for comprehensive endpoint validation.

Technical Details

  • JUnit 4 test runner with Spring integration
  • MockMvc for HTTP request simulation
  • SpringApplicationConfiguration with MockServletContext
  • WebAppConfiguration for web environment setup
  • Standalone controller testing configuration

Best Practices Demonstrated

The test implementation showcases clean testing practices with proper separation of concerns and test setup.

  • Isolated controller testing without dependencies
  • Clear test method naming conventions
  • Proper test setup using @Before annotation
  • Efficient use of Spring testing utilities

ityouknow/spring-boot-examples

2.x/spring-boot-package-war/src/test/java/com/neo/controller/HelloWorldControlerTests.java

            
package com.neo.controller;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
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;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class HelloWorldControlerTests {

    private MockMvc mvc;

    @Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();
    }

    @Test
    public void getHello() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
    }

}