我正在尝试为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测试中注入的吗?
下面是我的Test类(或多或少):
public class ServiceExampleTest {
@Test
public void testFoo() {
FooService fs = new FooServiceImpl();
Set<Foo> foos = fs.getFoos();
}
}这足以抛出异常。FooServiceImpl类返回一组Foo,其中Foo是一个实体类。当应用程序以通常的方式运行时,getFoos()方法会起作用。这个问题只出现在单元测试的上下文中。
发布于 2010-11-08 06:46:19
这是Spring Roo的一个令人难以置信的恼人问题,我还没有想出官方的解决方案。
但是..。以下是两种解决方法:
对于选项一,右键单击您的项目,选择属性-> AspectJ Build -> Aspect Path选项卡。
发布于 2010-11-12 01:08:28
庞杂是对的。通过让我的测试类扩展AbstractJunit4SpringContextTests,我能够拥有所有的spring注入魔术。
例如:
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" })
public class SelfRegistrationTest extends AbstractJUnit4SpringContextTests {发布于 2012-11-14 23:25:10
你的单元测试类应该有@MockStaticEntityMethods注解。
我只是想在上面@migue的回答中添加更多细节,因为我花了一段时间才弄清楚如何让它工作。网站http://java.dzone.com/articles/mock-static-methods-using-spring-aspects确实帮助我得出了下面的答案。
下面是我通过测试类注入实体管理器的方法。首先,使用@MockStaticEntityMethods注释您的测试类,并创建MockEntityManager类(这是一个仅实现EntityManager接口的类)。
然后,您可以在ServiceExampleTest测试类中执行以下操作:
@Test
public void testFoo() {
// call the static method that gets called by the method being tested in order to
// "record" it and then set the expected response when it is replayed during the test
Foo.entityManager();
MockEntityManager expectedEntityManager = new MockEntityManager() {
// TODO override what method you need to return whatever object you test needs
};
AnnotationDrivenStaticEntityMockingControl.expectReturn(expectedEntityManager);
FooService fs = new FooServiceImpl();
Set<Foo> foos = fs.getFoos();
}这意味着当您调用fs.getFoos()时,AnnotationDrivenStaticEntityMockingControl将注入您的模拟实体管理器,因为Foo.entityManager()是一个静态方法。
另请注意,如果 fs.getFoos()调用实体类上的其他静态方法(如Foo和Bar ),则它们也必须指定为此测试用例的一部分。
例如,假设Foo有一个名为"getAllBars(Long fooId)“的静态查找方法,当调用fs.getFoos()时会调用该方法,然后您需要执行以下操作才能使AnnotationDrivenStaticEntityMockingControl工作。
@Test
public void testFoo() {
// call the static method that gets called by the method being tested in order to
// "record" it and then set the expected response when it is replayed during the test
Foo.entityManager();
MockEntityManager expectedEntityManager = new MockEntityManager() {
// TODO override what method you need to return whatever object you test needs
};
AnnotationDrivenStaticEntityMockingControl.expectReturn(expectedEntityManager);
// call the static method that gets called by the method being tested in order to
// "record" it and then set the expected response when it is replayed during the test
Long fooId = 1L;
Foo.findAllBars(fooId);
List<Bars> expectedBars = new ArrayList<Bar>();
expectedBars.add(new Bar(1));
expectedBars.add(new Bar(2));
AnnotationDrivenStaticEntityMockingControl.expectReturn(expectedBars);
FooService fs = new FooServiceImpl();
Set<Foo> foos = fs.getFoos();
}请记住,AnnotationDrivenStaticEntityMockingControl的顺序必须与fs.getFoos()调用其静态方法的顺序相同。
https://stackoverflow.com/questions/3284496
复制相似问题