Controller Tests (GreetingControllerTests)
Relevant source files
The following files were used as context for generating this wiki page:- complete/src/test/java/com/example/restservice/GreetingControllerTests.java
- complete/src/main/java/com/example/restservice/GreetingController.java
- complete/src/main/java/com/example/restservice/Greeting.java
- complete/pom.xml
- complete/src/main/java/com/example/restservice/RestServiceApplication.java
- README.adoc
The GreetingControllerTests class provides integration tests for the GreetingController. Its primary purpose is to verify that the /greeting REST endpoint behaves correctly by sending mock HTTP requests and asserting the responses. These tests are crucial for ensuring the reliability of the service's main functionality without needing to run a full application server. The tests use Spring Boot's testing framework, specifically MockMvc, to simulate GET requests and validate the resulting JSON payload and HTTP status.
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java, README.adoc
Test Setup and Configuration
The test class is configured using annotations to set up a Spring test context and auto-configure MockMvc. This setup allows for focused testing of the web layer.
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:29-33
Annotations
@SpringBootTest: This annotation loads the full application context for an integration test. It starts the embedded server and creates the necessary beans, including theGreetingController.@AutoConfigureMockMvc: This annotation enables and auto-configuresMockMvc, a framework for testing Spring MVC controllers without a full HTTP server.
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:29-30
Dependencies
The testing capabilities are provided by the spring-boot-starter-test dependency, which is included in the pom.xml with a test scope. This starter transitively includes libraries like JUnit 5, Mockito, and Spring Test.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Sources: complete/pom.xml:28-32
Core Components
The central component for these tests is the MockMvc instance, which is injected into the test class via the @Autowired annotation. This object is used to perform requests against the controller.
@Autowired
private MockMvc mockMvc;
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:32-33
This diagram shows the main components involved in the test setup.
Test Cases
The GreetingControllerTests class contains two test methods, each verifying a specific scenario for the /greeting endpoint.
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:36-49
Test 1: Default Greeting
The noParamGreetingShouldReturnDefaultMessage test verifies the endpoint's behavior when no name parameter is provided. It expects the service to return a default greeting.
Logic:
- Perform a GET request to
/greeting. - Expect the HTTP status to be
200 OK. - Expect the JSON response body to contain a
contentfield with the value"Hello, World!".
@Test
public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, World!"));
}
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:36-41
The sequence diagram below illustrates the flow for this test case.
Test 2: Customized Greeting
The paramGreetingShouldReturnTailoredMessage test verifies the endpoint's behavior when a name parameter is provided. It expects the service to return a personalized greeting.
Logic:
- Perform a GET request to
/greetingwith anameparameter (e.g.,name=Spring Community). - Expect the HTTP status to be
200 OK. - Expect the JSON response body to contain a
contentfield with the value"Hello, Spring Community!".
@Test
public void paramGreetingShouldReturnTailoredMessage() throws Exception {
this.mockMvc.perform(get("/greeting").param("name", "Spring Community"))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, Spring Community!"));
}
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:44-49
The sequence diagram below illustrates the flow for this test case.
Summary of Test Methods
| Method Name | Description | Endpoint | Parameters | Expected Status | Expected Content |
|---|---|---|---|---|---|
noParamGreetingShouldReturnDefaultMessage | Tests the default greeting message. | /greeting | None | 200 OK | Hello, World! |
paramGreetingShouldReturnTailoredMessage | Tests the customized greeting message. | /greeting | name=Spring Community | 200 OK | Hello, Spring Community! |
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:36-49
Summary
The GreetingControllerTests class effectively validates the core functionality of the GreetingController through targeted integration tests. By using MockMvc, it confirms that the /greeting endpoint correctly handles requests both with and without parameters, returning the appropriate HTTP status and JSON response. These tests are a fundamental part of the application's quality assurance, ensuring the REST service behaves as documented and expected.
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java