2016-08-10 5 views
0

FreeMarkerをテンプレートエンジンとして使用するSpring Boot Webアプリケーションを開発しています。
私はspring-bootの1.4.0バージョンを使用しており、自動設定を使用しています。異なるロケールでは国際化があり、messages.propertiesはほとんどありません。現在のロケールアプリケーションに応じて、FreeMarkerによって生成されたHTMLが返され、適切な.propertiesの文字列で埋められます。FreemarkerでのSpring BootアプリケーションのユニットテストでNoSuchMessageExceptionがスローされる

しかし、正しく動作するコントローラをテストしようとすると、NoSuchMessageExceptionが得られ、FreeMarkerはmessages.propertiesに文字列を見つけることができません。

私はここでは、次の資料Testing improvements in Spring Boot 1.4

に応じてユニットテストを実施し、テストのコードです:

@RunWith(SpringRunner.class) 
@WebMvcTest(VideoController.class) 
public class VideoControllerTest { 

    @Autowired 
    private MockMvc mockMvc; 

    @MockBean 
    VideoService videoService; 

    @Test 
    public void showVideoSection() throws Exception { 
     ResultActions result = mockMvc.perform(MockMvcRequestBuilders.get("/video")); 
     result.andExpect(MockMvcResultMatchers.view().name("video")); 
    } 

} 

そしてここでは、テストメソッドのコードです:以下

@RequestMapping(value = "/video", method = RequestMethod.GET) 
public String showVideoSection(Model model) { 
    model.addAttribute("currentLanguage", LocaleContextHolder.getLocale().getLanguage()); 
    long numberOfAvailableVideos = videoService.getNumberOfAvailableVideos(); 
    if (numberOfAvailableVideos > 0) { 
     if (numberOfAvailableVideos >= 5) { 
      model.addAttribute("videoList", videoService.getPageOfVideos(0, 5)); 
     } else { 
      model.addAttribute("videoList", videoService.getPageOfVideos(0, (int)numberOfAvailableVideos)); 
     } 
    } 
    return "video"; 
} 

stack-ですトレース:

2016-08-10 17:07:32.210 ERROR 5504 --- [main] freemarker.runtime: Error executing FreeMarker template 

freemarker.core._TemplateModelException: Java method "org.springframework.web.servlet.support.RequestContext.getMessage(String)" threw an exception when invoked on org.springframework.web.servlet.support.RequestContext object "[email protected]"; see cause exception in the Java stack trace. 

---- 
FTL stack trace ("~" means nesting-related): 
- Failed at: ${springMacroRequestContext.getMessag... [in template "spring.ftl" in macro "message" at line 28, column 22] 
- Reached through: @spring.message code="nav.video" [in template "common.ftl" in macro "body" at line 65, column 49] 
- Reached through: @common.body mode="video" language="$... [in template "video.ftl" at line 7, column 5] 
~ Reached through: #nested [in template "common.ftl" in macro "html" at line 12, column 5] 
~ Reached through: @common.html [in template "video.ftl" at line 5, column 1] 
---- 

... 
... 
Caused by: org.springframework.context.NoSuchMessageException: No message found under code 'nav.video' for locale 'en'. 
at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:69) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1254) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
at org.springframework.web.servlet.support.RequestContext.getMessage(RequestContext.java:711) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
at org.springframework.web.servlet.support.RequestContext.getMessage(RequestContext.java:677) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77] 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77] 
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77] 
at freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:1458) ~[freemarker-2.3.25-incubating.jar:2.3.25] 
at freemarker.ext.beans.ReflectionCallableMemberDescriptor.invokeMethod(ReflectionCallableMemberDescriptor.java:56) ~[freemarker-2.3.25-incubating.jar:2.3.25] 
at freemarker.ext.beans.MemberAndArguments.invokeMethod(MemberAndArguments.java:51) ~[freemarker-2.3.25-incubating.jar:2.3.25] 
at freemarker.ext.beans.OverloadedMethodsModel.exec(OverloadedMethodsModel.java:61) ~[freemarker-2.3.25-incubating.jar:2.3.25] 
... 79 common frames omitted 

答えて

1

ファイルを正しく処理するには、MessageSourceAutoConfigurationを実行する必要があります。これはおそらく現時点では当てはまりません(そしてSpring Boot 1.4.1で修正する必要があります)。

次の内容のsrc/test/resources/META-INF/spring.factoriesファイルを追加することで問題を回避することができるかもしれませんが:

org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc=\ 
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration 

私はこの問題を解決するためにhttps://github.com/spring-projects/spring-boot/issues/6608を上げてきました。問題を再現するサンプルプロジェクトがある場合は、バグレポートへのリンクを添付してください。

+0

ありがとうございました! –

+0

残念ながら、 'spring.factories'を追加してもこの問題は解決されていません。私は春のブートで新しく、私はそれを使用して自分のサイトを作成しようとしています、ここで最後のコミット[リンク](https://github.com/Irgalin/klarigo/commit/5b004b928ad0c46c5b5a4d2a548ee88094b4df22)です。私はこの問題のために単一の支店を作りました、私はそれが私の愚かさに起因しないことを願っています。) –

+0

私はこれを働かせることができました...問題は 'spring.factories'が' src/main/test/resources/META-INF/'を実行します。同様にコミットされたファイルは、等号の後に改行を削除しました。すべてを1行にするには、改行文字を必要としません。 テストクラスの '@ImportAutoConfiguration(MessageSourceAutoConfiguration.class)'を使うことで、 '' MessageSourceAutoConfiguration''を1つか2つのテストで利用できるようにしたい場合は、 '@ImportAutoConfiguration(MessageSourceAutoConfiguration.class)'を使うことができます。 –

関連する問題