运行lagom测试时出现注入错误

时间:2017-10-13 22:25:20

标签: java jdbc lagom

我正在使用Lagom框架并通过mvn测试运行测试。 com.google.inject.CreationException测试失败。堆栈跟踪是

com.google.inject.CreationException: Unable to create injector, see 
the following errors:

1) No implementation for 
com.lightbend.lagom.javadsl.persistence.PersistentEntityRegistry was 
bound.
while locating 
com.lightbend.lagom.javadsl.persistence.PersistentEntityRegistry
for parameter 0 at com.pv.svc.chat.impl.ChatServiceImpl.<init>
(ChatServiceImpl.java:45)
at 
com.lightbend.lagom.javadsl.server.ServiceGuiceSupport.
bindServices(ServiceGuiceSupport.java:74) (via modules: 
com.google.inject.util.Modules$OverrideModule -> 
com.pv.svc.chat.impl.ChatServiceModule)

2) No implementation for 
com.lightbend.lagom.javadsl.persistence.ReadSide was bound.
while locating com.lightbend.lagom.javadsl.persistence.ReadSide
for parameter 1 at com.pv.svc.chat.impl.ChatServiceImpl.<init>
(ChatServiceImpl.java:45)
 at 
com.lightbend.lagom.javadsl.server.ServiceGuiceSupport.
bindServices(ServiceGuiceSupport.java:74) (via modules: 
com.google.inject.util.Modules$OverrideModule -> 
com.pv.svc.chat.impl.ChatServiceModule)

3) No implementation for 
com.lightbend.lagom.javadsl.persistence.jdbc.JdbcSession was bound.
while locating 
com.lightbend.lagom.javadsl.persistence.jdbc.JdbcSession
for parameter 2 at com.pv.svc.chat.impl.ChatServiceImpl.<init>
(ChatServiceImpl.java:45)
at 
com.lightbend.lagom.javadsl.server.ServiceGuiceSupport.
bindServices(ServiceGuiceSupport.java:74) (via modules: 
com.google.inject.util.Modules$OverrideModule -> 
com.pv.svc.chat.impl.ChatServiceModule)

3 errors
at 

com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist
(Errors.java:466)
at 
com.google.inject.internal.InternalInjectorCreator.  
initializeStatically(InternalInjectorCreator.java:155)
at 
com.google.inject.internal.InternalInjectorCreator.build
(InternalInjectorCreator.java:107)
at com.google.inject.Guice.createInjector(Guice.java:96)
at com.google.inject.Guice.createInjector(Guice.java:84)
at play.api.inject.guice.GuiceBuilder.injector
(GuiceInjectorBuilder.scala:181)
at play.inject.guice.GuiceBuilder.injector
(GuiceBuilder.java:208)
at play.inject.guice.GuiceApplicationBuilder.build
(GuiceApplicationBuilder.java:127)
at com.lightbend.lagom.javadsl.testkit.ServiceTest$.startServer
(ServiceTest.scala:282)
at com.lightbend.lagom.javadsl.testkit.ServiceTest.startServer
(ServiceTest.scala)
at com.pv.svc.chat.impl.ChatServiceTest.setUp
(ChatServiceTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0
(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall
(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run
(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively
(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate
(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate
(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute
(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet
(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke
(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0
(Native Method)
.....

我知道Lagom在运行测试时没有注入依赖项。我将JDBC用于持久实体和读取端。有没有办法解决这个问题。我的测试服务器设置

private static TestServer server;

@BeforeClass
public static void setUp() {
    server = ServiceTest.startServer(defaultSetup());
}

@AfterClass
public static void tearDown() {
    if (server != null) {
        server.stop();
        server = null;
    }
}

我知道对于Cassandra,我们必须使用defaultSetup()。withCassandra(true),但是JDBC呢?我也在使用Kakfa来接收消息。请帮忙。

2 个答案:

答案 0 :(得分:0)

您需要将Cassandra添加到您的设置

@BeforeClass
public static void setUp() {
    server = ServiceTest.startServer(defaultSetup().withCassandra(true));
}

答案 1 :(得分:0)

您可以将cassandra选项添加到build.sbt:

lagomCassandraEnabled in ThisBuild := true

和以下任意选项:

lagomCassandraPort in ThisBuild := 9042
lagomCassandraCleanOnStart in ThisBuild := true
lagomCassandraYamlFile in ThisBuild := Some((baseDirectory in ThisBuild).value / "project" / "cassandra.yml")

添加到Dependencies.scala(如果使用):

import com.lightbend.lagom.sbt.LagomImport.{......, lagomJavadslPersistenceCassandra}

并添加到服务实现的依赖项:

 val implYourServiceName= Seq(
    .......
    lagomJavadslPersistenceCassandra,
    ......
  )

将以下所有依赖项(没有依赖项文件)添加到build.sbt中:

lazy val `your-service-name-impl` = project("your-service-name-impl")
  .enablePlugins( .... your plugins here ....)
  .settings(libraryDependencies ++= Seq(
    .......
    lagomJavadslPersistenceCassandra,
    ......))