Back to Repositories

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

This test suite demonstrates Spring Boot REST API testing using MockMvc for a Hello World endpoint. It validates the basic functionality of a simple controller endpoint while showcasing Spring’s testing framework capabilities and JUnit integration.

Test Coverage Overview

The test coverage focuses on validating the HTTP GET endpoint ‘/hello’ response.

Key areas tested include:
  • HTTP response status code verification
  • Response content validation
  • Content type handling for JSON responses

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests without a full server deployment. The implementation leverages standalone setup with MockMvcBuilders, enabling isolated controller testing.

Technical patterns include:
  • MockMvc request building
  • Response content matching
  • HTTP status assertion
  • Request accept header specification

Technical Details

Testing tools and configuration:
  • SpringRunner for test execution
  • MockMvc for HTTP request simulation
  • JUnit 4 testing framework
  • Spring Boot Test annotations (@SpringBootTest)
  • MockMvcBuilders for test context setup
  • Hamcrest matchers for assertions

Best Practices Demonstrated

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

Notable practices include:
  • Proper test setup using @Before annotation
  • Clean separation of test configuration
  • Explicit content type handling
  • Clear assertion patterns
  • Focused test scope

ityouknow/spring-boot-examples

2.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")));
    }

}