gpt4 book ai didi

java - 使用 Guice 进行功能测试时出现 InstantiationException

转载 作者:行者123 更新时间:2023-11-28 20:53:23 26 4
gpt4 key购买 nike

我正尝试在 Play framework 2.4.6 中对我的 Controller 进行 Guice 测试。
以下代码工作正常:

public class GeneralTest {
@Inject
Application application;
private 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);
}

@After
public void teardown() {
Helpers.stop(application);
}

@Test
public void testLogin() throws SQLException {
RequestBuilder request = new RequestBuilder()
.uri(routes.LoginController.login().url());

running(application, () -> {
assertEquals(OK, route(request).status());
});
}


@Test
public void testCreateAccount() throws SQLException {
RequestBuilder request = new RequestBuilder()
.uri(routes.CreateController.createAccount().url());

running(application, () -> {
assertEquals(OK, route(request).status());
});
}
}

但是,当我尝试组织我的方法时,将 testLogin()testCreateAccount 分成两个测试类,它们由完全相同的 @Before@After,我得到了 InstantiationException,似乎在第一个之后执行的任何类上,发生在 RequestBuilder 的行上:

java.lang.InstantiationException: controllers.CreateController, took 2.656 sec
[error] at java.lang.Class.newInstance(Unknown Source)
[error] at play.api.inject.NewInstanceInjector$.instanceOf(Injector.scala:49)
[error] at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$15$$anonfun$apply$87.apply(Routes.scala:534)
[error] at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$15$$anonfun$apply$87.apply(Routes.scala:534)
[error] at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:136)
[error] at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:127)
[error] at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:70)
[error] at play.http.DefaultHttpRequestHandler$1.call(DefaultHttpRequestHandler.java:20)
[error] at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94)
[error] at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94)
[error] at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
[error] at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
[error] at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:40)
[error] at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70)
[error] at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32)
[error] at scala.concurrent.impl.Future$.apply(Future.scala:31)
[error] at scala.concurrent.Future$.apply(Future.scala:492)
[error] at play.core.j.JavaAction.apply(JavaAction.scala:94)
[error] at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$6.apply(Action.scala:108)
[error] at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$6.apply(Action.scala:108)
[error] at scala.Option.getOrElse(Option.scala:121)
[error] at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:107)
[error] at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:96)
[error] at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524)
[error] at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524)
[error] at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560)
[error] at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560)
[error] at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:536)
[error] at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:536)
[error] at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
[error] at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
[error] at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(Unknown Source)
[error] at java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
[error] at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(Unknown Source)
[error] at java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
[error] at java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
[error] Caused by: java.lang.NoSuchMethodException: controllers.CreateController.<init>()
[error] at java.lang.Class.getConstructor0(Unknown Source)
[error] ... 36 more

我正在关注这里的文档,它没有显示多个类的设置:
https://www.playframework.com/documentation/2.4.x/JavaTestingWithGuice https://www.playframework.com/documentation/2.4.x/JavaDependencyInjection

最佳答案

已更新
原因很简单,Play 正在并行执行测试。为确保它们串行执行,请将 parallelExecution in Global := false 放入 build.sbt 中。

如果你不想使用全局作用域,你可以改变作用域轴:
http://www.scala-sbt.org/0.13/docs/Scopes.html#Scoping+by+configuration+axis

下面的代码就完全不需要了:

running(application, () -> {
}

关于java - 使用 Guice 进行功能测试时出现 InstantiationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36086292/

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