使用Guice进行功能测试时的InstantiationException

时间:2016-03-18 13:57:02

标签: java testing playframework guice

我试图在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

1 个答案:

答案 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, () -> {
}