我们有一个 EAR 项目,其中包含多个子模块(多个 EJB、Web 项目、应用程序客户端等)。单一测试的自然范围是它们各自的子模块(因为它们应该测试独立的单元)。
在很短的时间内,我们引入了不明显的测试依赖项。项目正在模拟其他项目的功能,等等。很快我们的架构就发展到了几个带有模拟的独立 jar 文件(web 项目 1 模拟、ejb 2 模拟等);我们将这些模拟连接到 EAR 并使用子模块中的模拟(“Skinny War”风格)。
EAR
Modules
WEB 1
WEB 2
EJB 2
EJB 3
etc
Libs
Shared library 1
Shared Library 2
Testing dependencies
WEB 1 mocks
WEB 2 mocks
EJB 1 mocks
EJB 2 mocks
etc
WEB1
Uses EJB 1 and EJB 3
Uses Shared Library 1
Testing
Consumes EJB 1 and EJB 2 mocks
无论如何,我们团队的共识是模拟正在失控。我们希望向 Arquillian 发展并在容器内进行测试(例如,向集成测试)。我们还推出了 ATTD(最初只是使用 Drone 进行功能测试,但我希望尽快拥有功能齐全的 Thucydidies + JBehave 或 EasyB 设置)。
测试可能依赖于多个子模块的资源。 ShrinkWrap 可以保证事情不会失控。
所以我的问题是:我应该在哪里放置测试、故事、Arquillian 配置文件等?
我觉得 EAR 是对所有内容进行分组的最佳场所:
EAR
Modules
Test
src
Java
Test 1
Test 2
Resources
Configuration Files
Stories
Story 1
Story 2
这将使我们能够拥有一个统一的报告,忘记模块间的依赖关系,拥有单点配置等等。
但我可能是错的(使用每个模块的粒度有其优点)。
那么 Arquillian 测试的最佳实践是什么:我应该将测试文件放在 EAR 项目中吗?我应该将集成/验收测试放在 EAR 项目中,并将单一测试放在子模块中吗?或者我应该把所有东西都放在子模块中?
<小时/>更新:替代方法。我应该isolate integration tests到一个单独的模块?如果是这样,如何(我应该如何设置依赖项、配置 Arquillian 等)?
最佳答案
让我提供一些有关如何使用 Maven 组织集成测试的实用信息,以便其他陷入困境的人可以做正确的事情™。
我最终遵循了来自梦幻般的人(即使有点老)的建议Better Builds with Maven以及Codehaus Maven and Integration Testing Guide .
使用聚合器顶级项目:
myproject myproject-ear myproject-war myproject-ejb ... myproject-integration-tests
如suggested by mchamati ,让每个模块通过单元测试进行 self 测试(就像以前一样)。单元测试速度很快,并且可以在每个构建上运行。
- 同样对于单元测试,事实证明我最初的策略并没有那么糟糕。我仍然有几个模拟模块作为托管依赖项绑定(bind)到 EAR。
有独立的集成测试模块,其封装类型可以是pom; (您不会从该项目中构建真正的可部署 Artifact ;此外,当测试数量开始增长时,将其转变为另一个聚合器 pom 可能是有意义的):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId>myproject</artifactId> <groupId>com.mycompany</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.myproject</groupId> <artifactId>myproject-integration-tests</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging>
遵循 Maven 约定集成测试应进入
src/it
:<build> <testSourceDirectory>src/it</testSourceDirectory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> <executions> <execution> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin>
使用failsafe运行集成测试:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.17</version> <configuration> <encoding>${project.build.sourceEncoding}</encoding> </configuration> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.17</version> </dependency> </dependencies> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin>
让您的集成测试项目导入 Ear 项目依赖项(以及您需要的其他所有内容)。我不确定这是否被认为是好的做法,因为任何指南中都没有提到它,但它对我来说非常有效。
<dependency> <groupId>com.mycompany</groupId> <artifactId>my-project-ear</artifactId> <version>1.0-SNAPSHOT</version> <type>pom</type> </dependency>
集中 arquillian集成测试模块中的相关配置:
<dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.arquillian</groupId> <artifactId>arquillian-bom</artifactId> <version>${arquillian.version}</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.junit</groupId> <artifactId>arquillian-junit-container</artifactId> <version>${arquillian.version}</version> <scope>test</scope> </dependency> <!-- Arquillian container related dependencies, etc -->
关注failsafe naming conventions用于测试文件。
src/it/java com/mycompany/myproject mypackage MyIT.java MySecondIT.java anotherpackage YetAnotherIT.java
利润
在顶级聚合器项目下:
mvn test
⇒ 运行充满模拟的快速单元测试mvn verify
⇒ 运行真正的集成/功能/验收测试(可能会很慢)
额外提示:如果您正在运行 CI 服务器,例如 Jenkins设置您的夜间构建来运行集成测试(即,让您的构建调用 mvn verify
)。请勿将未经验证的版本部署到认证或生产服务器。
关于java - Maven文件夹布局: Should I place tests in the EAR or its sub-modules?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24517814/