3

ローカライズをテストしようとしています。私の構成は以下のとおりです

  @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());
  }

テスト方法は以下のようになります

  @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)));
  }

リクエストを介してロケールを正常に変更できましたが、テストが機能していないようです。ここで何が欠けているのか、誰かが私を案内してくれませんか。

4

0 に答える 0