I'm trying to apply spring internationalization in my project.
I'm using AcceptHeaderLocaleResolver.
When testing one Test with accept-header, it works properly But when testing multiple Tests, it doesn't work
This is my test code
@Test
void test1() {
...
builder = MockMvcRequestBuilders.post("/path")
.contentType(MediaType.APPLICATION_JSON)
.locale(Locale.ENGLISH)
.content(mapper.writeValueAsString(request));
ResultActions resultActions = mockMvc.perform(builder);
//then
resultActions
.andDo(print());
}
@Test
void test2() {
...
builder = MockMvcRequestBuilders.post("/path")
.contentType(MediaType.APPLICATION_JSON)
.locale(Locale.KOREA)
.content(mapper.writeValueAsString(request));
ResultActions resultActions = mockMvc.perform(builder);
//then
resultActions
.andDo(print());
}
Environment
@Bean
public LocaleResolver localeResolver() {
AcceptHeaderLocaleResolver acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();
acceptHeaderLocaleResolver.setDefaultLocale(Locale.ENGLISH);
acceptHeaderLocaleResolver.setSupportedLocales(Arrays.asList(Locale.KOREA));
return acceptHeaderLocaleResolver;
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource rs = new ResourceBundleMessageSource();
rs.setBasename("messages");
rs.setDefaultEncoding("UTF-8");
rs.setUseCodeAsDefaultMessage(false);
return rs;
}
when testing test2, it print messages which translated in korean
but when testing test1 and test2 in same time test2 print message which not translated in korean.
I can't find reason why it works like that.
CodePudding user response:
I tried to get Message From enum which initiated by MessageSource. It didn't work.
I change my logic to use MessageSource directly and it works