Back to Repositories

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

This test suite demonstrates Spring Boot REST controller testing using MockMvc for the HelloWorld endpoint. It validates basic HTTP GET request handling and response status verification in a standalone test environment using Spring’s testing framework.

Test Coverage Overview

The test suite provides coverage for the HelloWorld controller’s GET endpoint functionality.

Key areas tested include:
  • HTTP GET request handling for ‘/hello’ endpoint
  • Response status code verification
  • 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 follows a standalone setup pattern with MockMvcBuilders, enabling isolated controller testing.

Notable technical implementations:
  • MockMvc standalone configuration
  • SpringRunner test execution
  • Request builder pattern usage

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Spring Boot Test context
  • MockMvc for request simulation
  • MockMvcResultHandlers for response logging
  • MockMvcResultMatchers for assertions
  • Standalone controller setup without WebApplicationContext

Best Practices Demonstrated

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

Notable practices include:
  • Clean test setup using @Before annotation
  • Isolated controller testing through standalone setup
  • Clear test method naming conventions
  • Proper use of Spring Boot testing annotations
  • Explicit media type specifications

ityouknow/spring-boot-examples

2.x/spring-boot-helloWorld/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.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.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(SpringRunner.class)
@SpringBootTest
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();
    }

}