Back to Repositories

Testing Spring Boot REST Endpoints with MockMvc in spring-boot-examples

This test suite demonstrates Spring Boot REST API testing using MockMvc and JUnit. It focuses on validating a simple Hello World endpoint through mock HTTP requests and response validation.

Test Coverage Overview

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

Key areas tested include:
  • HTTP GET request handling
  • Response status code verification
  • Response content validation
  • 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 leverages standalone setup with MockMvcBuilders, allowing isolated controller testing.

Notable patterns include:
  • MockMvc request building
  • Fluent assertion chaining
  • Spring test context configuration

Technical Details

Testing infrastructure includes:
  • SpringJUnit4ClassRunner for test execution
  • MockServletContext for web environment simulation
  • MockMvc for request handling
  • Hamcrest matchers for assertions
  • WebAppConfiguration for web context setup

Best Practices Demonstrated

The test class exemplifies several testing best practices in Spring Boot applications.

Notable practices include:
  • Proper test setup isolation using @Before
  • Clear test method naming
  • Focused test scope
  • Use of mock objects to avoid external dependencies
  • Explicit content type and response validation

ityouknow/spring-boot-examples

1.x/spring-boot-package-war/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.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
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(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
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")));
    }

}