【发布时间】:2016-01-16 12:58:41
【问题描述】:
我想学习并尝试建立一个项目使用
- 播放框架2.4
- JAVA
- 弹簧数据
- Eclipse IDE
我的“灵感”来自
- play-scala-spring-data-jpa 展示如何通过 guice 加载 spring 数据。
- multiproject 显示子项目配置(因为我想使用至少两个具有共同代码库的应用程序,但应该单独打包,以便能够部署到不同的机器上)
我成功地完成了activator run 我的第一个子项目,这是一个play-scala-spring-data-jpa 的JAVA 端口。来自 spring 数据的注入魔法适用于提供的示例 (models.PersonRepository)。
现在我在测试方面陷入困境。当我想加载和测试控制器类(例如controllers.Application 的功能测试)时,我无法设置测试类并注入models.PersonRepository,但只能在运行测试时(s ) 我的子项目core out of Eclipse IDE(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);
}
虽然我正在阅读 playframework 手册 (starting here) 的测试相关页面,但我找不到任何有用的信息。
当我在 Eclipse IDE 之外进行测试时收到该错误,
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 Spring 模块。其次,我必须确保 configs.AppConfig 已正确加载,因为此类管理 spring 数据配置。
由于我对这一切完全陌生,也许有人可以帮助我,展示一下
- 做什么,让我的测试在 Eclipse IDE(Scala IDE 插件)用完时也能正常工作
- 如何强制提供的测试类使用专用的内存测试数据库(不一定要使用为我的子项目
core/conf/META-INF/persistence.xml定义的数据库)。
完整源码here
编辑
我发现,当从 Eclipse IDE 使用 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 的项目,我想知道我还需要做什么才能获得匹配的类路径
【问题讨论】:
标签: java spring guice playframework-2.4 scala-ide