Back to Repositories

Testing Spring Boot REST Endpoints in spring-boot-examples

This test suite demonstrates Spring Boot REST endpoint testing using MockMvc for HTTP request simulation and response validation. It focuses on testing a simple Hello World controller endpoint while showcasing Spring’s testing framework capabilities.

Test Coverage Overview

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

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 deploying to a server. The implementation demonstrates standalone controller testing with MockMvcBuilders, allowing isolated unit tests of web layer components.

Technical patterns include:
  • MockMvc request builder pattern
  • Fluent assertion chaining
  • Standalone setup configuration

Technical Details

Testing tools and configuration:
  • SpringJUnit4ClassRunner for test execution
  • WebAppConfiguration for web context setup
  • MockMvc for request simulation
  • Hamcrest matchers for assertions
  • MediaType specifications for content negotiation

Best Practices Demonstrated

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

Notable practices include:
  • Proper test setup isolation using @Before
  • Clear test method naming
  • Specific assertion checking
  • Clean separation of test configuration
  • Efficient use of mock objects

ityouknow/spring-boot-examples

spring-boot-package/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.http.MediaType;
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)
@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")));
    }

}