Back to Repositories

Testing Spring Boot Hello Controller Endpoints in spring-boot-examples

This test suite validates the functionality of a Spring Boot Hello World controller using JUnit and MockMvc. It demonstrates proper setup and execution of HTTP endpoint testing in a Spring Boot application with actuator capabilities.

Test Coverage Overview

The test suite focuses on validating the basic HTTP GET endpoint ‘/hello’ functionality.

Key areas covered include:
  • HTTP response status verification
  • Endpoint accessibility testing
  • JSON content type handling
  • Basic request-response cycle validation

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests without requiring a full server deployment. The implementation leverages standalone setup with MockMvcBuilders, enabling isolated controller testing.

Technical patterns include:
  • MockMvc request building
  • Response status assertion
  • Request result printing for debugging
  • MediaType specification for JSON responses

Technical Details

Testing tools and configuration:
  • JUnit 4 test runner
  • SpringRunner for Spring context integration
  • MockMvc for HTTP request simulation
  • SpringBootTest annotation for application context
  • Standalone controller setup without WebApplicationContext
  • MockMvcRequestBuilders for request construction
  • MockMvcResultMatchers for response validation

Best Practices Demonstrated

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

Notable practices include:
  • Proper test isolation using standalone setup
  • Clear test method naming conventions
  • Setup method separation using @Before annotation
  • Explicit media type specification
  • Proper exception handling
  • Clean and maintainable test structure

ityouknow/spring-boot-examples

2.x/spring-boot-actuator/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.test.context.junit4.SpringRunner;
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 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();
    }

}