Spring Local Rest Language Test not working

I am trying to check localization. My configuration is below

  @Bean
  public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource =
        new ReloadableResourceBundleMessageSource();
    String[] resources = {"/WEB-INF/i18n/messages"};
    messageSource.setBasenames(resources);
    messageSource.setFallbackToSystemLocale(true);
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
  }

  @Bean
  public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("lang");
    return localeChangeInterceptor;
  }

  @Bean
  public LocaleResolver localeResolver() {
    CookieLocaleResolver localeResolver = new CookieLocaleResolver();
    localeResolver.setDefaultLocale(Locale.ENGLISH);
    return localeResolver;
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
  }

      

The testing method looks like below

  @Test
  public void testParamLang() throws Exception {
    log.info("Param Test");
    HttpHeaders headers = new HttpHeaders();
    mockMvc
        .perform(
            get(URL).contentType(MediaType.APPLICATION_JSON)
                .param("lang", locale.getLanguage()).param(MSG_KEY, MSG_KEY_VAL))
        .andExpect(status().isOk()).andDo(print())
        .andExpect(content().string(messageSource.getMessage(MSG_KEY_VAL, null, locale)));
  }

      

I can successfully change the locale via a request, but the test doesn't seem to work. Can anyone please guide me to what I am missing here.

+3


source to share





All Articles