Back to Repositories

Testing Spring Boot REST Controller Response Handling in spring-boot-examples

This test suite demonstrates Spring Boot REST controller testing using MockMvc for a basic Hello World endpoint. It validates the HTTP response and content matching through Spring’s testing framework while showcasing standalone controller test configuration.

Test Coverage Overview

The test suite focuses on validating a simple REST endpoint’s behavior in isolation.

Key areas covered include:
  • HTTP GET request handling
  • Response status code verification
  • Response content validation
  • JSON media type acceptance

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests without a full server deployment. The implementation leverages standalone setup pattern with MockMvcBuilders, enabling focused controller testing without loading the complete application context.

Key patterns include:
  • MockMvc request building
  • Fluent assertion chaining
  • Standalone controller initialization

Technical Details

Testing stack components:
  • SpringRunner for test execution
  • MockMvc for request simulation
  • SpringBootTest annotation for configuration
  • JUnit 4 as the base testing framework
  • Hamcrest matchers for assertions
  • MockMvcRequestBuilders for request construction

Best Practices Demonstrated

The test class exemplifies several testing best practices in the Spring ecosystem.

Notable practices include:
  • Proper test setup isolation using @Before
  • Clear test method naming
  • Focused controller testing without dependencies
  • Explicit media type specification
  • Clean assertion structure

ityouknow/spring-boot-examples

1.x/spring-boot-helloWorld/src/test/java/com/neo/controller/HelloTests.java

            
package com.neo.controller;

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

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloTests {

	
    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(status().isOk())
                .andExpect(content().string(equalTo("Hello World")));
    }

}