我试图在Guice
中对我的控制器进行Play framework 2.4.6
测试
以下代码工作正常:
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
分成2个由完全相同的@Before
和@After
组成的测试类,I'得到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
答案 0 :(得分:0)
<强>更新强>
原因很简单,Play正在并行执行测试。为了确保它们以串行方式执行,请将parallelExecution in Global := false
放入build.sbt
。
如果您不想使用全局范围,可以更改范围轴:
http://www.scala-sbt.org/0.13/docs/Scopes.html#Scoping+by+configuration+axis
以下代码完全不:
running(application, () -> {
}