Sub topic 16
Testing the generated documentation
This chapter assumes you have springdoc-openapi running in a Spring Boot 3 project, know the /v3/api-docs endpoint, and can write a basic Spring Boot test with JUnit 5. The goal is to catch documentation regressions in the build, so a broken or missing description fails a test instead of reaching a consumer.
Why test the documentation
The OpenAPI document is generated from your code at runtime. A renamed path, a removed annotation, or a changed springdoc.paths-to-match value can silently change what clients see. A test that fetches the document and checks key facts turns the specification into a checked contract.
A MockMvc test
The test below uses @SpringBootTest with @AutoConfigureMockMvc. It requests the document and checks the title, one path, and one schema. The assertions use JSONPath, which the jsonPath matcher supports.
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class OpenApiDocumentationTest {
@Autowired
private MockMvc mockMvc;
@Test
void documentDescribesTheLibraryApi() throws Exception {
mockMvc.perform(get("/v3/api-docs"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.openapi").value(org.hamcrest.Matchers.startsWith("3.")))
.andExpect(jsonPath("$.info.title").value("Library API"))
.andExpect(jsonPath("$.paths['/api/books/{id}'].get.summary").value("Get a book by id"))
.andExpect(jsonPath("$.components.schemas.Book.properties.title").exists());
}
}Use bracket notation for path keys such as ['/api/books/{id}'], because they contain slashes and braces.
What to assert
Test facts that clients depend on, and avoid comparing the whole document.
- Metadata: title and version from your
OpenAPIbean. - Paths and methods: the endpoints you promise to expose.
- Responses: documented status codes such as 404 on
GET /api/books/{id}. - Schemas: required fields and constraints derived from Bean Validation.
- Security: the
bearerAuthscheme undercomponents.securitySchemes. - Exclusions: internal paths hidden with
@Hiddendo not appear.
An exclusion check uses doesNotExist(). The path /internal/health here is a placeholder, so substitute one of your own hidden endpoints.
mockMvc.perform(get("/v3/api-docs"))
.andExpect(jsonPath("$.paths['/internal/health']").doesNotExist());Common errors
| Symptom | Likely cause |
|---|---|
| 404 on `/v3/api-docs` in the test | Test profile sets `springdoc.api-docs.enabled=false`, or the path was changed |
| 401 or 403 in the test | Spring Security blocks the endpoint and the test sends no credentials |
| Schema missing from `components.schemas` | The type is never referenced by an endpoint, or it is hidden |
| Test fails after a harmless refactor | Assertions compare too much, such as the full JSON text |
| Path key not found | JSONPath written with dots instead of bracket notation |
Because the production profile disables the documentation, run this test under the default or test profile, not prod.
Exercise
A test calls GET /v3/api-docs and gets 404, although the endpoint works when you run the application normally. Name two causes to check first.
Show answer
The test profile may disable the docs (springdoc.api-docs.enabled=false, for example inherited from a prod-style configuration), or the springdoc.api-docs.path property may have been changed so the document is served at a different URL. Checking the active profile and the configured path resolves most cases.