Back to Repositories

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

This test suite validates the HelloController endpoints in a Spring Boot web application using JUnit and MockMvc. It demonstrates comprehensive testing of REST endpoints with proper request handling and response validation.

Test Coverage Overview

The test suite provides coverage for the HelloController’s HTTP GET endpoint ‘/hello’.

Key functionality tested includes:
  • Endpoint accessibility and HTTP status code verification
  • Response content validation against expected ‘Hello World’ string
  • JSON media type acceptance testing

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests without deploying to a server.

Key patterns include:
  • Standalone setup of MockMvc with isolated controller testing
  • Request builder pattern for HTTP request construction
  • Fluent assertion API for response validation

Technical Details

Testing tools and configuration:
  • JUnit 4 with SpringRunner for test execution
  • MockMvc for REST endpoint testing
  • SpringBootTest annotation for Spring context configuration
  • MockMvcBuilders for test infrastructure setup
  • Hamcrest matchers for response content validation

Best Practices Demonstrated

The test suite exhibits several testing best practices:
  • Proper test setup isolation using @Before
  • Clear test method naming conventions
  • Separate tests for different validation aspects
  • Use of fluent assertions for readable test cases
  • Proper mock setup and teardown management

ityouknow/spring-boot-examples

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


}