Back to Repositories

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

This test suite demonstrates Spring Boot REST API testing using MockMvc and JUnit. It validates the basic functionality of a Hello World endpoint through automated unit tests, ensuring proper HTTP responses and content matching.

Test Coverage Overview

The test suite provides coverage for a basic REST endpoint implementation in Spring Boot.

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

Implementation Analysis

The testing approach utilizes Spring’s WebMvcTest annotation for focused controller testing without loading the full application context. The implementation leverages MockMvc for simulating HTTP requests and validating responses, demonstrating clean separation of concerns and efficient test execution.

Notable patterns include:
  • Spring Runner integration
  • Autowired test components
  • Fluent assertion chain
  • Request builder pattern

Technical Details

Testing infrastructure includes:
  • JUnit 4 test framework
  • Spring Test context framework
  • MockMvc for request simulation
  • Hamcrest matchers for assertions
  • Spring Boot test autoconfiguration
  • MediaType specifications for JSON handling

Best Practices Demonstrated

The test implementation showcases several testing best practices in the Spring ecosystem.

Notable practices include:
  • Focused test scope using @WebMvcTest
  • Clear test method naming
  • Proper exception handling
  • Explicit content type specification
  • Response status verification
  • Content matching validation

ityouknow/spring-boot-examples

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

            
package com.neo.controller;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
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 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)
@WebMvcTest(HelloWorldController.class)
public class HelloTests {

    @Autowired
    private MockMvc mvc;

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

}