[英]How to switch locale in Spring REST docs?
我有一个@RestController
,其中控制器方法的参数之一是Locale
@RequestMapping("/{id}")
public Survey getSurvey( @PathVariable("id") SurveyId surveyId,
Locale locale ) { ... }
我有一个正在运行的集成测试(使用RestAssured),可以通过设置Accept-Language
标头来切换语言环境。
我现在也想使用Spring REST文档对此进行记录。 在这种情况下(使用MockMvc)设置标题无效。
我的测试是这样的:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration
@WebAppConfiguration
public void SurveyControllerDocumentation {
// Test methods here
...
// Application context for documentation test
@Configuration
@EnableAutoConfiguration
public static class TestConfiguration {
@Bean
public SurveyController controller(MessageSource messageSource) {
return new SurveyController(userService(), messageSource, surveyService());
}
@Bean
public UserService userService() {
return mock(UserService.class);
}
@Bean
public SurveyService surveyService() {
return mock(SurveyService.class);
}
@Bean
public CustomEditorsControllerAdvice customEditorsControllerAdvice() {
return new CustomEditorsControllerAdvice();
}
@Bean
public RestResponseEntityExceptionHandler exceptionHandler() {
return new RestResponseEntityExceptionHandler();
}
}
}
是否有一些我需要显式添加到进行语言环境注入的测试上下文中的bean?
我正在使用Spring Boot 1.3.3(具有Spring 4.2.5)
您可以设置Locale
使用locale(Locale)
的要求构建器方法:
mockMvc.perform(
get("/")
.accept(MediaType.APPLICATION_JSON)
.locale(Locale.GERMAN))
.andExpect(status().isOk())
.andDo(document("example"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.