gpt4 book ai didi

java - Controller 测试 - CacheManager 已关闭。无法再使用

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:03 24 4
gpt4 key购买 nike

我在尝试使用 GuicePlay Framework 2.4.6 中进行 Controller 测试时遇到了标题所示的错误。它发生在 Controller 中的任何 view.render 代码中。 redirect 不会产生此问题。

堆栈:

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

以下是我的设置:

Controller :

@Singleton
public class AccountController extends Controller {
private AccountService accountService;

@Inject
public Controller(AccountService a) {
accountService = a;
}

public Result addAccount() {
boolean success = accountService.addAccount();
if (success)
return ok(CreateAccount.render());//<--THIS TRIGGERS THE ERROR WHEN RUNNING THE TEST
}
}

界面:

@ImplementedBy(AccountServiceImpl.class)
public interface AccountService {
boolean addAccount();
}

实现:

public class AccountServiceImpl implements AccountService {
@Override
public boolean addAccount() {
}
}

测试:

public class TestClass {
@Inject
Application application;

final AccountService accountServiceMock = mock(AccountService.class);

@Before
public void setup() {
Module testModule = new AbstractModule() {
@Override
public void configure() {
bind(AccountService.class).toInstance(accountServiceMock);
}
};

GuiceApplicationBuilder builder = new GuiceApplicationLoader()
.builder(new ApplicationLoader.Context(Environment.simple()))
.overrides(testModule);
Guice.createInjector(builder.applicationModule()).injectMembers(this);

Helpers.start(application);
}

@Test
public void testMethod() throws Exception {
RequestBuilder request = new RequestBuilder()
.session("userId", "1")
.uri(controllers.routes.AccountController.addAccount().url());

running(application, () -> {
when(accountServiceMock.addAccount().thenReturn(true);
assertEquals(OK, route(request).status());
});

}

感谢任何帮助!

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

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

更准确地说:

"userId" // <--- THIS. userIds, or any other I've tried, solved the issue.

再次恢复,只要在测试方法中为RequestBuilder设置了userId session ,并在Controller中设置了view.render,就会出现这个错误。

无论我是否在 html 中甚至在 Controller 中使用此类 session 。有人知道这里发生了什么吗?

最佳答案

将测试方法内的所有内容移至running 调用内:

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 method停止应用程序,然后在路由请求时您的 assert 中会出现错误。

关于java - Controller 测试 - CacheManager 已关闭。无法再使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35833443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com