Java 控制器测试-CacheManager已关闭。它不能再使用了

Java 控制器测试-CacheManager已关闭。它不能再使用了,java,playframework,guice,Java,Playframework,Guice,我在尝试使用Guice在Play framework 2.4.6中进行控制器测试时遇到了标题所述的错误。它与任何视图一起出现。在控制器中渲染代码重定向不会产生此问题 堆栈: 1) Error in custom provider, java.lang.IllegalStateException: The CacheManager has been shut down. It can no longer be used. at play.api.cache.EhCacheModule.play

我在尝试使用
Guice
Play framework 2.4.6
中进行控制器测试时遇到了标题所述的错误。它与任何
视图一起出现。在控制器中渲染
代码<代码>重定向
不会产生此问题

堆栈:

1) Error in custom provider, java.lang.IllegalStateException: The CacheManager has been shut down. It can no longer be used.
  at play.api.cache.EhCacheModule.play$api$cache$EhCacheModule$$bindCache$1(Cache.scala:178):
Binding(interface net.sf.ehcache.Ehcache qualified with QualifierInstance(@play.cache.NamedCache(value=play)) to ProviderTarget(play.api.cache.NamedEhCacheProvider@4c8f1e9)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
  while locating net.sf.ehcache.Ehcache annotated with @play.cache.NamedCache(value=play)
  at play.api.cache.EhCacheModule.play$api$cache$EhCacheModule$$bindCache$1(Cache.scala:179):
Binding(interface play.api.cache.CacheApi qualified with QualifierInstance(@play.cache.NamedCache(value=play)) to ProviderTarget(play.api.cache.NamedCacheApiProvider@38fd7da7)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
  while locating play.api.cache.CacheApi annotated with @play.cache.NamedCache(value=play)
  while locating play.api.cache.CacheApi
    for parameter 0 at play.cache.DefaultCacheApi.<init>(DefaultCacheApi.java:20)
  at play.cache.DefaultCacheApi.class(DefaultCacheApi.java:20)
  while locating play.cache.DefaultCacheApi
  while locating play.cache.CacheApi 
"userId" // <--- THIS. userIds, or any other I've tried, solved the issue.
实施:

1) Error in custom provider, java.lang.IllegalStateException: The CacheManager has been shut down. It can no longer be used.
  at play.api.cache.EhCacheModule.play$api$cache$EhCacheModule$$bindCache$1(Cache.scala:178):
Binding(interface net.sf.ehcache.Ehcache qualified with QualifierInstance(@play.cache.NamedCache(value=play)) to ProviderTarget(play.api.cache.NamedEhCacheProvider@4c8f1e9)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
  while locating net.sf.ehcache.Ehcache annotated with @play.cache.NamedCache(value=play)
  at play.api.cache.EhCacheModule.play$api$cache$EhCacheModule$$bindCache$1(Cache.scala:179):
Binding(interface play.api.cache.CacheApi qualified with QualifierInstance(@play.cache.NamedCache(value=play)) to ProviderTarget(play.api.cache.NamedCacheApiProvider@38fd7da7)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
  while locating play.api.cache.CacheApi annotated with @play.cache.NamedCache(value=play)
  while locating play.api.cache.CacheApi
    for parameter 0 at play.cache.DefaultCacheApi.<init>(DefaultCacheApi.java:20)
  at play.cache.DefaultCacheApi.class(DefaultCacheApi.java:20)
  while locating play.cache.DefaultCacheApi
  while locating play.cache.CacheApi 
"userId" // <--- THIS. userIds, or any other I've tried, solved the issue.
测试:

1) Error in custom provider, java.lang.IllegalStateException: The CacheManager has been shut down. It can no longer be used.
  at play.api.cache.EhCacheModule.play$api$cache$EhCacheModule$$bindCache$1(Cache.scala:178):
Binding(interface net.sf.ehcache.Ehcache qualified with QualifierInstance(@play.cache.NamedCache(value=play)) to ProviderTarget(play.api.cache.NamedEhCacheProvider@4c8f1e9)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
  while locating net.sf.ehcache.Ehcache annotated with @play.cache.NamedCache(value=play)
  at play.api.cache.EhCacheModule.play$api$cache$EhCacheModule$$bindCache$1(Cache.scala:179):
Binding(interface play.api.cache.CacheApi qualified with QualifierInstance(@play.cache.NamedCache(value=play)) to ProviderTarget(play.api.cache.NamedCacheApiProvider@38fd7da7)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
  while locating play.api.cache.CacheApi annotated with @play.cache.NamedCache(value=play)
  while locating play.api.cache.CacheApi
    for parameter 0 at play.cache.DefaultCacheApi.<init>(DefaultCacheApi.java:20)
  at play.cache.DefaultCacheApi.class(DefaultCacheApi.java:20)
  while locating play.cache.DefaultCacheApi
  while locating play.cache.CacheApi 
"userId" // <--- THIS. userIds, or any other I've tried, solved the issue.
感谢您的帮助

编辑:我已经查明了导致问题的确切原因,但我仍然不知道原因。

RequestBuilder request = new RequestBuilder()
            .session("userId", "1") // <--- THIS IS CAUSING THE PROBLEM
            .uri(controllers.routes.AccountController.addAccount().url());
RequestBuilder request=newrequestbuilder()

.session(“userId”,“1”)//将测试方法中的所有内容移动到运行调用的
中:

running(application, () -> {
    RequestBuilder request = new RequestBuilder()
        .session("userId", "1")
        .uri(controllers.routes.AccountController.addAccount().url());

    when(accountServiceMock.addAccount().thenReturn(true);
    assertEquals(OK, route(request).status());
});

停止应用程序,然后在路由请求时,您的
断言中出现错误。

将测试方法中的所有内容移动到
正在运行的调用中:

running(application, () -> {
    RequestBuilder request = new RequestBuilder()
        .session("userId", "1")
        .uri(controllers.routes.AccountController.addAccount().url());

    when(accountServiceMock.addAccount().thenReturn(true);
    assertEquals(OK, route(request).status());
});

停止应用程序,然后在路由请求时,您的
断言中出现错误。

在哪里实例化应用程序?您不应该在(classLoader).in(Mode.TEST).build()中执行类似于
application=new-GuiceApplicationBuilder()的操作吗
?@Kris我相信所有需要的设置都已经在前面用
@注释的设置方法中了。再次实例化
应用程序
将导致
无法执行游戏。应用程序已绑定。
也许您是对的。无论如何,我在我的测试用例中启动一个FakeApplication
ClassLoader=FakeApplication.class.getClassLoader();application=new-GuiceApplicationBuilder().in(classLoader).in(Mode.TEST).build();助手。启动(应用程序)。但在我看来,在编写游戏中的测试时,需要更多的尝试和错误。如果有效,我会坚持下去。:)@Kris评论了我的代码并测试了你的代码,还有
.overrides(testModule)
。导致了相同的CacheManager错误。在哪里实例化应用程序?您不应该在(classLoader).in(Mode.TEST).build()中执行类似于
application=new-GuiceApplicationBuilder()的操作吗
?@Kris我相信所有需要的设置都已经在前面用
@注释的设置方法中了。再次实例化
应用程序
将导致
无法执行游戏。应用程序已绑定。
也许您是对的。无论如何,我在我的测试用例中启动一个FakeApplication
ClassLoader=FakeApplication.class.getClassLoader();application=new-GuiceApplicationBuilder().in(classLoader).in(Mode.TEST).build();助手。启动(应用程序)。但在我看来,在编写游戏中的测试时,需要更多的尝试和错误。如果有效,我会坚持下去。:)@Kris评论了我的代码并测试了你的代码,还有
.overrides(testModule)
。导致了相同的CacheManager错误。非常抱歉!我不小心复制粘贴了旧版本的代码。我已经编辑了这个问题-assertEquals实际上正在运行。如果不是,它将导致
java.lang.RuntimeException:没有启动的应用程序。我现在尝试将
RequestBuilder
放入其中,但仍然导致CacheManager错误。非常抱歉!我不小心复制粘贴了旧版本的代码。我已经编辑了这个问题-assertEquals实际上正在运行。如果不是,它将导致
java.lang.RuntimeException:没有启动的应用程序。我现在尝试将
RequestBuilder
放入其中,但仍然会导致CacheManager错误。