Testing Spring Boot REST Controller Responses in spring-boot-examples
This test suite demonstrates Spring Boot REST API testing using MockMvc and JUnit. It validates the basic functionality of a Hello World endpoint through automated unit tests, ensuring proper HTTP responses and content matching.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
ityouknow/spring-boot-examples
spring-boot-helloWorld/src/test/java/com/neo/controller/HelloTests.java
package com.neo.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
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 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(SpringRunner.class)
@WebMvcTest(HelloWorldController.class)
public class HelloTests {
@Autowired
private MockMvc mvc;
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders
.get("/hello")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print())
.andExpect(content()
.string(equalTo("Hello World")));
}
}