Back to Repositories

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

This test suite validates the functionality of a Spring Boot web controller using JUnit and MockMvc. It focuses on testing HTTP GET endpoints and response handling, demonstrating proper integration testing practices for RESTful web services.

Test Coverage Overview

The test suite provides comprehensive coverage of the HelloController’s endpoint functionality.

  • Tests HTTP GET requests to /hello endpoint
  • Validates response status codes and content
  • Verifies correct response body content matching
  • Covers basic happy path scenarios for API responses

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests and validating responses. The implementation employs standalone setup for controller testing, isolating the web layer from the full application context.

Key patterns include:
  • MockMvc request building and execution
  • Response content validation
  • Status code verification
  • Content type handling with MediaType.APPLICATION_JSON

Technical Details

Testing tools and configuration:

  • JUnit 4 test runner with SpringRunner
  • MockMvc for HTTP request simulation
  • @SpringBootTest annotation for Spring context
  • MockMvcBuilders for standalone controller setup
  • Hamcrest matchers for response validation
  • MockMvcResultHandlers for request/response logging

Best Practices Demonstrated

The test suite exemplifies several testing best practices for Spring Boot applications.

  • Proper test initialization with @Before setup
  • Isolated controller testing using standalone setup
  • Clear test method naming conventions
  • Comprehensive response validation
  • Use of fluent assertion API
  • Proper exception handling in test methods

ityouknow/spring-boot-examples

1.x/spring-boot-web/src/test/java/com/neo/web/HelloControlerTests.java

            
package com.neo.web;

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;

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;

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;

import com.neo.web.HelloController;

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

    private MockMvc mvc;

    @Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
    }

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

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


}