2

I'm trying to set up integrated testing for my simple Java EE app that uses JAX-RS to expose a REST service (it's also an EJB) and JPA to store the underlying data, and I continue to struggle finding a solution that doesn't require manually setting up an external container/application server. I've done a lot of searching here and on the web for solutions, and ended up burning far too many weeks to get GlassFish 3.1.2 Embedded working, which I succeed doing until the final straw a week ago: conflicting documentation and bugs trying to create users to test security. Drat! I looked at JBoss AS 7, but the embedded product looks unsupported. I then tried rolling my own using a servlet container (Jetty, Grizzly, and Simple) to directly run Jersey. (I was eventually planning on adding in EclipseLink and Derby embedded.) All of these presented unexpected and blocking challenges in setting up and running the most basic JUnit tests; I couldn't get Jetty to run JAX-RS, and Grizzly didn't have HTTP Basic authentication, for example. Double drat! I then looked at Arquillian, but couldn't find JBoss AS 7 listed in their container adapters.

I have two questions: 1) This seems to be far, far harder to get working than I expected. If I assume it's not just me (please, it's not just me!), then why is it so hard? I'm wondering if the entire Java EE stack with its 30+ APIs is a house of cards that's very difficult to get stacked up right and doesn't lend itself to being shoved into a single jar. Or maybe people prefer to do integration testing on an external ("local"?) container that they start before running the tests.

Question 2): What is the standard terminology for the different kinds of test-container configurations? It seems inconsistent. For example, http://mvnrepository.com/artifact/org.jboss.arquillian.container lists "remote", "embedded", "managed", and "local", but yet I found "in-container" and "out-of-container" testing mentioned elsewhere.

Rather than use space on details on what failed above, I'm ready to listen to what this experience is trying to tell me, so I would really appreciate hearing what worked for you. Thanks in advance!

4

2 回答 2

1

我每天都有完全相同的问题。我的结论是:将业务层与服务接口层解耦。即:通过对业务层进行良好设计进行单元测试,并在需要时使用外部工具进行集成测试(例如 jmeter 或任何可以在已部署系统上进行功能测试的工具)。

大多数时候,我想测试业务逻辑,而不是服务抽象层。我的 jax-rs(或其他)处理程序通常只是业务层的包装器。

顺便说一句,我从未使用过 Arquillian。

我不知道这是否可以被认为是一个答案,这只是我的 2 美分;)

于 2012-06-08T17:10:55.910 回答
0

你正在尝试做的事情听起来像是我已经做了一段时间的事情了。

我目前正在使用Arquillian与 JBoss AS 7 一起运行,因为这是我们在生产中使用的应用程序服务器。Arquillian 支持托管模式(Arquillian 控制容器的启动和停止)、远程模式(Arquillian 不启动或停止服务器,但假定它在某处运行)和嵌入式模式。并非所有容器适配器都支持嵌入式模式,而且我所理解的 Glassfish 嵌入式有其公平份额的问题。Apache TomEE 提供了一个嵌入式容器,我在各种环境中都使用过它,而且效果很好。TomEE 适配器的一个好处是它由实际的 TomEE 团队维护,因此他们可以确保适配器始终与他们的容器兼容。TomEE 的启动/关闭速度非常快,因此测试基本上与单元测试一样快,差异几乎不明显。

<dependency>
    <groupId>org.apache.openejb</groupId>
    <artifactId>arquillian-tomee-embedded</artifactId>
    <version>1.0.0</version>
    <scope>provided</scope>
</dependency>

JBoss AS 7 在其测试套件中使用 Arquillian 本身,因此您无需担心没有针对该容器测试 Java EE API 和实现。如果您希望构建是独立的并包含 JBoss AS 7 实例,您可以使用 jboss-as-dist 工件。如果您使用 Maven,您可以使用 maven-dependency-plugin 解压缩 JBoss AS 7 实例以用于您的测试套件:

<build>
  <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
          <execution>
              <id>unpack</id>
              <phase>process-test-classes</phase>
              <goals>
                  <goal>unpack</goal>
              </goals>
              <configuration>
                  <artifactItems>
                      <artifactItem>
                          <groupId>org.jboss.as</groupId>
                          <artifactId>jboss-as-dist</artifactId>
                          <version>7.1.1.Final</version>
                          <type>zip</type>
                          <overWrite>false</overWrite>
                          <outputDirectory>target</outputDirectory>
                      </artifactItem>
                  </artifactItems>
              </configuration>
          </execution>
      </executions>
  </plugin>
</build>

(此处为 pom.xml 的完整示例)

如果您的代码在生产环境中的应用程序服务器中运行,但您没有进行集成测试,那么您真的无法确定您的代码在实际运行时中的行为方式。感谢 Arquillian,我已经能够在我最近的项目中编写集成测试,这些测试在应用程序服务器中执行,测试从通过 http 的 JAX-RS 端点、JPA ORM 映射和查询甚至到 NoSQL 数据库的业务逻辑和索引引擎。

如果您遇到 Arquillian的问题, Arquillian 用户论坛会很有帮助。希望我的回答能对你的问题有所启发。

于 2013-05-14T13:40:16.560 回答