Sub topic 12
Testing versioned endpoints
This chapter assumes you can write Spring MVC controllers and @SpringBootTest tests, and that you have the AccountController and header-based configuration from the earlier chapters. The goal is to prove that each version reaches the right handler and that bad versions are rejected.
What a versioned test needs to cover
Versioning adds a routing decision before your handler runs. A test should check that decision, not only the response body. Four cases cover most of the risk:
- An explicit version reaches the matching handler.
- A request with no version gets the default version.
- An unsupported version is rejected.
- A baseline range such as
2.0+still matches a later supported version.
Sending the version with MockMvc
MockMvc has no special versioning API. The version is part of the request, so you set it the same way a client would. With the header resolver, that means .header("X-API-Version", "2.0"). If you use a path resolver, put the version in the URL. If you use a query parameter resolver, use .param(...).
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.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 AccountVersioningTests {
@Autowired
MockMvc mockMvc;
@Test
void version2ReturnsSplitName() throws Exception {
mockMvc.perform(get("/accounts/1").header("X-API-Version", "2.0"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.firstName").value("Ada"));
}
@Test
void version1ReturnsSingleName() throws Exception {
mockMvc.perform(get("/accounts/1").header("X-API-Version", "1.0"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Ada Lovelace"));
}
@Test
void missingVersionFallsBackToDefault() throws Exception {
mockMvc.perform(get("/accounts/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Ada Lovelace"));
}
@Test
void unsupportedVersionIsRejected() throws Exception {
mockMvc.perform(get("/accounts/1").header("X-API-Version", "3.0"))
.andExpect(status().isBadRequest());
}
}The default-version test passes only because the configuration calls setDefaultVersion("1.0"). If you remove the default, the same request fails with an error. That makes this test a useful guard against someone changing the default by accident.
Checking the shape, not only the status
Both versions return 200, so a status check alone cannot tell them apart. Assert on a field that exists in only one version. In the tests above, $.name identifies version 1.0 and $.firstName identifies version 2.0. You can also use jsonPath("$.name").doesNotExist() on the version 2.0 response to confirm the old shape is gone.
Common mistakes
- Wrong header name. The name in the test must match the one in
useRequestHeader(...), including spelling. A mismatched name is ignored, and the request silently gets the default version. - Version missing from the supported list. A version you map but forget to add to
addSupportedVersionsis rejected before it reaches the controller. - Testing a different configuration than production. If you configure versioning in
application.properties, a slice test such as@WebMvcTestmay not load yourWebMvcConfigurer. Use@SpringBootTestor import the configuration class.
Exercise
A test sends GET /accounts/1 with the header "X-API-Versoin: 2.0" (misspelled) and expects the version 2.0 body, but the response has a "name" field instead. Why, and how would you make the test fail loudly?
Show answer
The resolver reads only "X-API-Version", so it never sees the misspelled header. The request has no version, and the configured default "1.0" is used, which returns the version 1.0 shape. Fix the header name. To fail loudly, assert on a version-specific field such as jsonPath("$.firstName") instead of only the status code.