Spring Rest语言环境测试不起作用

时间:2014-12-18 14:46:15

标签: java spring spring-test-mvc

我正在尝试测试本地化。我的配置如下所示

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

我能够通过请求成功更改语言环境,但测试似乎不起作用。有人可以指导我,我在这里错过了什么。

0 个答案:

没有答案
相关问题