Back to Repositories

Testing Spring MVC Controller Endpoints in spring-boot-examples

This test suite demonstrates Spring MVC controller testing using MockMvc for a Hello World endpoint. It validates the basic HTTP response functionality and request handling in a Spring Boot application using JUnit and Spring Test framework.

Test Coverage Overview

The test suite focuses on validating the HelloWorldController’s HTTP GET endpoint functionality.

Key areas covered include:
  • HTTP GET request handling for ‘/hello’ endpoint
  • Response status code verification
  • JSON media type acceptance testing
  • Request-response cycle validation

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests without a full server deployment. It implements a standalone setup pattern with mock servlet context, enabling isolated controller testing.

Framework-specific features utilized:
  • SpringJUnit4ClassRunner for test execution
  • MockMvcBuilders for test context setup
  • MockMvcRequestBuilders for request simulation
  • MockMvcResultMatchers for response validation

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Spring Boot Test framework
  • MockMvc for MVC testing
  • MockServletContext for web environment simulation
  • @WebAppConfiguration for web context testing
  • @SpringApplicationConfiguration for test context configuration

Best Practices Demonstrated

The test implementation showcases several testing best practices in Spring Boot applications.

Notable practices include:
  • Proper test isolation using standalone setup
  • Clear test method naming conventions
  • Efficient test context configuration
  • Clean setup separation using @Before annotation
  • Explicit media type specification
  • Comprehensive response validation

ityouknow/spring-boot-examples

1.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();
    }

}