【发布时间】:2010-07-19 19:52:47
【问题描述】:
我正在尝试为 Spring Roo 项目编写 JUnit 测试。如果我的测试需要使用实体类,我会得到以下异常:
java.lang.IllegalStateException: Entity manager has not been injected
(is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)
Spring Aspects JAR 看起来配置正确。特别是,我在pom.xml 文件中有以下内容:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
和
<plugin>
<configuration>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
当没有从 JUnit 测试中调用时,使用实体类的类可以正常工作。知道如何设置以便从 JUnit 测试中注入实体管理器吗?
这是我的测试课(或多或少):
public class ServiceExampleTest {
@Test
public void testFoo() {
FooService fs = new FooServiceImpl();
Set<Foo> foos = fs.getFoos();
}
}
这足以引发异常。 FooServiceImpl 类返回一组 Foo,其中 Foo 是一个实体类。当应用程序以通常的方式运行时,getFoos() 方法起作用。问题只出现在单元测试的上下文中。
【问题讨论】:
-
您也可以发布您的测试课程吗?我从未使用过 Spring Roo,但对于普通的 Spring 测试,您通常必须扩展 AbstractSpringJUnit4Test(或其他东西)或通过注释使用自定义 Spring 运行器进行测试。
标签: java junit spring-roo entitymanager