我想学习并尝试用
我的灵感来源于
我成功地activator run了我的第一个子项目,它是播放-scala-spring-data-jpa的JAVA端口。来自spring数据的注入魔术适用于所提供的示例(models.PersonRepository)。
现在我在测试的时候被困住了。当我想加载和测试一个控制器类(例如,一个controllers.Application的功能测试)、时,我无法成功地设置一个测试类并注入controllers.Application,但只在运行出Eclipse 的子项目 core 的测试时(core是通过执行activator eclipse创建的eclipse项目)。执行activator test可以工作,下面显示的测试成功运行。
public class ApplicationTest extends WithApplication {
@Inject private PersonRepository repo;
@Inject Application application;
@Before
public void setup() {
// Here supposes to happen more to make PersonRepository injection work when running JUnit tests out of Eclipse IDE
GuiceApplicationBuilder builder = new GuiceApplicationLoader()
.builder(new Context(Environment.simple()));
Guice.createInjector(builder.applicationModule()).injectMembers(this);
}
@After
public void teardown() {
Helpers.stop(application);
}
@Test
public void testInjection() {
assertNotNull(repo);
}虽然我正在阅读与测试相关的剧本手册(从这里开始),但我找不到任何有用的东西。
当我在Eclipse中测试时收到这个错误时,
com.google.inject.ConfigurationException: Guice configuration errors:
1) No implementation for models.PersonRepository was bound.
while locating models.PersonRepository
for field at core.ApplicationTest.repo(ApplicationTest.java:32)
while locating core.ApplicationTest因此,我认为问题在于,我必须引用我的子项目的应用程序配置(core/conf/core-application.conf),它为我的子项目启用了Guice模块。其次,我必须确保适当地加载了configs.AppConfig,因为这个类管理spring数据配置。
因为我对这一切都很陌生,也许有人可以帮我,展示一下
core/conf/META-INF/persistence.xml定义的测试数据库)。全源代码这里
编辑--我发现,在使用Eclipse的JUnit测试而不是activator test时,类路径是不同的,所以我假设这就是注入不起作用的原因。
# Classpath output was sorted before
$ diff activatorCP eclipseCP
0a1
>
88,89d88
< /h/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.6.jar
< /h/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.6.jar
130,136c129,131
< /h/.sbt/boot/scala-2.10.5/org.scala-sbt/sbt/0.13.9/test-agent-0.13.9.jar
< /h/.sbt/boot/scala-2.10.5/org.scala-sbt/sbt/0.13.9/test-interface-1.0.jar
< /h/workspace/springtest/core/target/scala-2.11/classes/
< /h/workspace/springtest/core/target/scala-2.11/test-classes/
< /h/workspace/springtest/core/target/web/classes/main/
< /h/workspace/springtest/core/target/web/classes/test
< /h/workspace/springtest/core/target/web/public/test/
---
> /h/workspace/springtest/core/bin/
> /opt/eclipse-mars/configuration/org.eclipse.osgi/213/0/.cp/
> /opt/eclipse-mars/configuration/org.eclipse.osgi/214/0/.cp/由于我已经使用activator compile eclipse创建了eclipse的项目,我想知道我还需要做什么,才能获得匹配的类路径
发布于 2016-02-05 14:01:36
行为上的差异来自于在项目的build.sbt中自定义activator test选项,从而允许activator test正确运行。这些定制必须手动转到eclipse项目的运行配置中。之后,eclipse之外的测试也成功运行。
https://stackoverflow.com/questions/34827227
复制相似问题