Application Context Tests
Relevant source files
The following files were used as context for generating this wiki page:- complete/src/test/java/com/example/restservice/RestServiceApplicationTests.java
- initial/src/test/java/com/example/restservice/RestServiceApplicationTests.java
- complete/src/test/java/com/example/restservice/GreetingControllerTests.java
- complete/pom.xml
- complete/src/main/java/com/example/restservice/RestServiceApplication.java
- test/run.sh
Application Context Tests are a form of integration testing used in this project to validate the Spring application's configuration and component wiring. These tests load the entire Spring ApplicationContext to ensure that the application can start up correctly and that its components, such as controllers, are properly instantiated and configured. This approach provides a high degree of confidence that the application's core is functioning as a whole.
The primary mechanism for these tests is the @SpringBootTest annotation, which bootstraps the application defined in RestServiceApplication. The tests range from a simple context loading "smoke test" to more complex tests that mock HTTP requests to the web layer and verify the responses.
Sources: initial/src/test/java/com/example/restservice/RestServiceApplicationTests.java:8, complete/src/test/java/com/example/restservice/GreetingControllerTests.java:29, complete/src/main/java/com/example/restservice/RestServiceApplication.java
Test Dependencies
The foundation for all testing, including context tests, is provided by the spring-boot-starter-test dependency. This starter package bundles key testing libraries.
| Group ID | Artifact ID | Scope | Purpose |
|---|---|---|---|
org.springframework.boot | spring-boot-starter-test | test | Provides core Spring Boot testing utilities, including Spring Test, JUnit 5, Mockito, and Hamcrest. |
This dependency is defined in the project's Maven configuration.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Sources: complete/pom.xml:28-32
Basic Context Loading Test
The most fundamental application context test is designed to verify that the Spring context can be loaded without errors. This acts as a smoke test for the application's configuration.
Sources: initial/src/test/java/com/example/restservice/RestServiceApplicationTests.java
Implementation
The RestServiceApplicationTests class uses the @SpringBootTest annotation to trigger the loading of the application context. The test method contextLoads() is empty; the test passes if the application context is successfully loaded and fails if any exceptions occur during startup (e.g., bean creation errors, configuration problems).
package com.example.restservice;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class RestServiceApplicationTests {
@Test
public void contextLoads() {
}
}
Sources: initial/src/test/java/com/example/restservice/RestServiceApplicationTests.java:3-13
Execution Flow
The following diagram illustrates the process of the contextLoads test.
This diagram shows that the test's success is determined by the successful loading of the ApplicationContext before the test method itself is even executed.
Sources: initial/src/test/java/com/example/restservice/RestServiceApplicationTests.java:8,11
Controller Integration Test
A more advanced context test, GreetingControllerTests, not only loads the application context but also tests the web layer by sending mock HTTP requests to the GreetingController. This verifies the end-to-end flow from request mapping to response generation without needing to run a full web server.
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java
Key Annotations and Components
| Annotation / Class | Purpose |
|---|---|
@SpringBootTest | Loads the full Spring application context for an integration test. |
@AutoConfigureMockMvc | Configures a MockMvc instance, which provides a way to test controllers without a running HTTP server. |
MockMvc | The main entry point for server-side Spring MVC test support. It is autowired into the test class. |
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:29-30, 33
Test Implementation
The GreetingControllerTests class contains two test methods that use an autowired MockMvc instance to perform GET requests to the /greeting endpoint.
noParamGreetingShouldReturnDefaultMessage(): Tests the endpoint without any parameters and expects the default message "Hello, World!".paramGreetingShouldReturnTailoredMessage(): Tests the endpoint with anameparameter and expects a customized message.
The tests use MockMvcResultMatchers like status().isOk() and jsonPath(...) to assert the HTTP status code and the content of the JSON response body.
@SpringBootTest
@AutoConfigureMockMvc
public class GreetingControllerTests {
@Autowired
private MockMvc mockMvc;
@Test
public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, World!"));
}
@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:29-51
Request-Response Flow
This diagram shows the sequence of events when a MockMvc test is executed.
This flow demonstrates how MockMvc simulates the Spring MVC request-processing pipeline within the test environment.
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:39-41, 47-49, complete/src/main/java/com/example/restservice/GreetingController.java:13
Test Execution Script
The project includes a shell script run.sh that automates the build and test process. It uses Maven (./mvnw) to clean, compile, and package the application, which implicitly runs the test suite. This script is indicative of how tests are executed in a continuous integration environment.
cd ../complete
./mvnw clean package
Sources: test/run.sh:15-16