Maven文件夹布局:我应该在EAR或其子模块中放置测试吗?

Ant*_*oly 0 java integration-testing module maven jboss-arquillian

我们有一个包含多个子模块的EAR项目(多个EJB,Web项目,应用程序客户端等).单一测试的自然范围是它们各自的子模块(因为它们应该是测试隔离单元).

在很短的时间内,我们引入了非明显的测试依赖项.项目正在嘲笑来自其他项目的功能等.很快我们的架构演变成了几个带有模拟的独立jar文件(web项目1个模拟,ejb 2模拟等); 我们将这些模拟与EAR连接起来并消耗子模块中的模拟("瘦战"风格).

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
Run Code Online (Sandbox Code Playgroud)

无论如何,我们团队的共识是模拟失控.我们希望向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
Run Code Online (Sandbox Code Playgroud)

这将允许我们有一个统一的报告,忘记模块间的依赖关系,有一个单一的配置点等等.

但我可能错了(使用每个模块的粒度有其优点).

那么Arquillian测试的最佳实践是什么:我应该将我的测试文件放在EAR项目中吗?我应该在EAR项目中进行集成/验收测试吗?在子模块中进行单一测试吗?或者我应该把所有东西都放在子模块中?


更新:替代方法.我应该将集成测试隔离到单独的模块中吗?如果是这样,如何(如何设置依赖关系,配置Arquillian等)?

Ant*_*oly 7

让我提供一些关于如何使用Maven组织集成测试的实用信息,以便其他人可以使用它来做正确的事情.

我最终接受了来自Maven以及Codehaus Maven和集成测试指南的出色建议(即使有点旧)的建议.

  1. 使用聚合器顶级项目:

    myproject
        myproject-ear
        myproject-war
        myproject-ejb
        ...
        myproject-integration-tests
    
    Run Code Online (Sandbox Code Playgroud)
  2. 正如mchamati建议的,让每个模块都使用单元测试进行测试(就像之前一样).单元测试很快,可以在每个构建上运行.

  3. 同样对于单元测试,事实证明我最初的策略并不是那么糟糕.我仍然有几个模拟模块绑定到EAR作为托管依赖项.
  4. 有一个单独的集成测试模块,其包装类型可以是pom; (你不会在这个项目中构建一个真正可部署的工件;而且,当测试数量开始增长时,将它转换为另一个聚合器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>
    
    Run Code Online (Sandbox Code Playgroud)
  5. 遵循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> 
    
    Run Code Online (Sandbox Code Playgroud)
  6. 使用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>
    
    Run Code Online (Sandbox Code Playgroud)
  7. 让您的集成测试项目导入ear项目依赖项(以及您需要的所有其他内容).我不确定这是否被认为是好的做法,因为它没有在任何指南中提及,但它对我来说非常好.

    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>my-project-ear</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>pom</type>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)
  8. 在集成测试模块中集中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 -->
    
    Run Code Online (Sandbox Code Playgroud)
  9. 遵循测试文件的故障安全命名约定.

    src/it/java
        com/mycompany/myproject
            mypackage
                MyIT.java 
                MySecondIT.java
            anotherpackage
                YetAnotherIT.java  
    
    Run Code Online (Sandbox Code Playgroud)
  10. 利润

    在顶级聚合器项目下:

    1. mvn test ⇒运行充满嘲讽的快速单元测试
    2. mvn verify ⇒运行真正的集成/功能/验收测试(可能很慢)

额外提示:如果您正在运行诸如Jenkins之类的CI服务器,请设置每晚构建以运行集成测试(即,进行构建调用mvn verify).不要将未经验证的构建部署到认证或生产服务器.