如何在Spring Roo中使用JUnit测试? (EntityManager的问题) [英] How to use JUnit tests with Spring Roo? (Problems with EntityManager)

查看:688
本文介绍了如何在Spring Roo中使用JUnit测试? (EntityManager的问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write a JUnit test for a Spring Roo project. If my test requires use of the entity classes, I get the following Exception:

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 文件中有以下内容:

The Spring Aspects JAR looks to be configured correctly. In particular, I have the following in the pom.xml file:

<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测试中注入实体管理器吗?

and the classes that use the entity classes work fine, when not called from a JUnit test. Any idea how I can set things up so that the Entity manager is injected from a JUnit test?

这是我的Test类(或多或少):

Here is my Test class (more or less):

public class ServiceExampleTest {

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

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

This is enough to throw the exception. The FooServiceImpl class returns a Set of Foo, where Foo is an entity class. The getFoos() method works when the application is run in the usual way. The problem only comes in the context of unit tests.

推荐答案

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

This is an incredibly annoying problem with Spring Roo and I have not figured out the official solution for.

但是......这里有两个解决方法:

But ... here are two workarounds:


  • 将spring-aspects jar复制到项目中,然后将其添加到Projects AspectJ Aspect Path

  • 使用Maven运行单元测试(并错过绿色栏:()

对于选项1右键单击您的项目,选择Properties-> AspectJ Build - > Aspect Path Tab。

For option one Right click on your project select Properties-> AspectJ Build -> Aspect Path Tab.

这篇关于如何在Spring Roo中使用JUnit测试? (EntityManager的问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆