首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Spring Roo中使用JUnit测试?(EntityManager的问题)

如何在Spring Roo中使用JUnit测试?(EntityManager的问题)
EN

Stack Overflow用户
提问于 2010-07-20 03:52:48
回答 7查看 7.2K关注 0票数 6

我正在尝试为Spring Roo项目编写一个JUnit测试。如果我的测试需要使用实体类,我会得到以下异常:

代码语言:javascript
复制
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文件中有以下内容:

代码语言:javascript
复制
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>${spring.version}</version>
</dependency>

代码语言:javascript
复制
<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类(或多或少):

代码语言:javascript
复制
public class ServiceExampleTest {

  @Test
  public void testFoo() {
    FooService fs = new FooServiceImpl();
    Set<Foo> foos = fs.getFoos();
  }
}

这足以抛出异常。FooServiceImpl类返回一组Foo,其中Foo是一个实体类。当应用程序以通常的方式运行时,getFoos()方法会起作用。这个问题只出现在单元测试的上下文中。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-11-08 06:46:19

这是Spring Roo的一个令人难以置信的恼人问题,我还没有想出官方的解决方案。

但是..。以下是两种解决方法:

  • 将spring-aspects jar复制到您的项目中,然后将其添加到您的项目中AspectJ Aspect Path
  • 使用Maven运行您的单元测试(并且错过绿色条:( )

对于选项一,右键单击您的项目,选择属性-> AspectJ Build -> Aspect Path选项卡。

票数 3
EN

Stack Overflow用户

发布于 2010-11-12 01:08:28

庞杂是对的。通过让我的测试类扩展AbstractJunit4SpringContextTests,我能够拥有所有的spring注入魔术。

例如:

代码语言:javascript
复制
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" })
public class SelfRegistrationTest extends AbstractJUnit4SpringContextTests {
票数 6
EN

Stack Overflow用户

发布于 2012-11-14 23:25:10

你的单元测试类应该有@MockStaticEntityMethods注解。

我只是想在上面@migue的回答中添加更多细节,因为我花了一段时间才弄清楚如何让它工作。网站http://java.dzone.com/articles/mock-static-methods-using-spring-aspects确实帮助我得出了下面的答案。

下面是我通过测试类注入实体管理器的方法。首先,使用@MockStaticEntityMethods注释您的测试类,并创建MockEntityManager类(这是一个仅实现EntityManager接口的类)。

然后,您可以在ServiceExampleTest测试类中执行以下操作:

代码语言:javascript
复制
@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工作。

代码语言:javascript
复制
@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()调用其静态方法的顺序相同。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3284496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档