简体   繁体   English

Eclipse JAVA单独的单元测试和集成测试

[英]Eclipse JAVA separate unit test and integration test

im upgrading a project that contain only integration test wrote in JAVA. 我正在升级仅包含用JAVA编写的集成测试的项目。

Now we are going to write unit test, so i decide to create src/it/java folder to put all existing tests and wrote new unit test in src/test/java 现在我们要编写单元测试,所以我决定创建src / it / java文件夹以放置所有现有测试,并在src / test / java中编写新的单元测试

I have use surfire and build-helper to do that. 我使用surfire和build-helper来做到这一点。

in command line i can run 在命令行中我可以运行

mvn clean install and only src/test/java directory are read for test mvn全新安装,仅读取src / test / java目录进行测试

when im doing 我在做什么

mvn -Pintegration-test clean install i see that test in src/it/java are read for test mvn -Pintegration-test全新安装,我看到已读取src / it / java中的测试以进行测试

so all work fine in command line. 因此所有命令都可以在命令行中正常运行。

Now i would like to import my project on eclipe. 现在,我想在eclipe上导入我的项目。 And when im doing that only src/test/java is use as source folder. 当我这样做时,只有src / test / java用作源文件夹。 How can i set up pom.xml to have src/test/java and src/it/java as source folder? 如何设置pom.xml以将src / test / java和src / it / java作为源文件夹?

**here is my builder plugin conf **这是我的构建器插件配置

<plugin>    
<groupId>org.codehaus.mojo</groupId>     
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>         
    <execution>             
      <id>add-test-source</id>       
      <phase>process-resources</phase>             
      <goals>                 
           <goal>add-test-source</goal>             
      </goals>             
      <configuration>                 
             <sources>                     
       <source>src/it/java</source>                 
         </sources>             
      </configuration>         
    </execution>     
</executions>
</plugin> 

thanks a lot! 非常感谢!

There are in general three ways of solving such problem. 通常,有三种解决该问题的方法。

Using a separate folder for integration tests as you already decided to use. 您已经决定使用的单独文件夹用于集成测试。

.
|-- pom.xml
`-- src
    |-- it
    |   `-- java
    |       `-- com
    |           `-- soebes
    |               `-- maui
    |                   `-- it
    |                       `-- BitMaskIT.java
    |-- main
    |   `-- java
    |       `-- com
    |           `-- soebes
    |               `-- maui
    |                   `-- it
    |                       `-- BitMask.java
    `-- test
        `-- java
            `-- com
                `-- soebes
                    `-- maui
                        `-- it
                            `-- BitMaskTest.java

To get this working correctly in Eclipse you need to use the build-helper-maven-plugin which you already suggested: 为了使它在Eclipse中正常工作,您需要使用您已经建议的build-helper-maven-plugin:

 <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
      <execution>
        <id>add-test-source</id>
        <!--
            This will help m2eclipse to recognize the folder as source
            folder after update project configuration.
            Based on comment updated.
        -->
        <phase>validate</phase>
        <goals>
          <goal>add-test-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>src/it/java</source>
          </sources>
        </configuration>
      </execution>
    </executions>
  </plugin>

But as you can see this takes several supplemental configuration in your pom file. 但是如您所见,这需要在pom文件中进行几个补充配置。

Apart from the above the usage of maven-surefire-plugin for integration tests is simply wrong, cause maven-surefire-plugin is intended for unit tests and not for integration tests so better is to go with maven-failsafe-plugin for integration tests. 除了上述以外,将maven-surefire-plugin用于集成测试是完全错误的,因为maven-surefire-plugin用于单元测试而非集成测试,因此最好将maven-failsafe-plugin用于集成测试。

Best is to following the naming conventions for unit and integration tests. 最好是遵循单元测试和集成测试的命名约定。 In maven-surefire-plugin the unit tests follow the following convention : maven-surefire-plugin中,单元测试遵循以下约定

- *Test.java
- *TestCase.java
- Test*.java

whereas the integration tests in maven-failsafe-plugin follow the following convention: maven-failsafe-plugin中的集成测试遵循以下约定:

- *IT.java
- *ITCase.java
- IT*.java
.
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- soebes
    |               `-- maui
    |                   `-- it
    |                       `-- BitMask.java
    `-- test
        `-- java
            `-- com
                `-- soebes
                    `-- maui
                        `-- it
                            |-- BitMaskIT.java
                            `-- BitMaskTest.java

But sometimes much more better to make a separate module which contains only the integration tests to separate them from the unit tests. 但是有时制作一个单独的模块更好一些,该模块仅包含集成测试以将其与单元测试分开。 This might be helpfull if the integration tests have different configuration than the unit tests. 如果集成测试的配置与单元测试的配置不同,这可能会有所帮助。

So making the structure like this: 因此,使结构如下所示:

 +-- root (pom.xml)
     +--- mod-it (pom.xml)
     +--- mod-core (pom.xml)

You should follow the naming convention in the mod-it to use the maven-failsafe-plugin and correctly execute integration tests in the integration-test phase of the maven build life-cycle. 您应该遵循mod-it的命名约定,以使用maven-failsafe-plugin并在maven构建生命周期的integration-test阶段正确执行集成测试。

You will need to add the src/it/java folder manually in Eclipse via: 您将需要通过以下方式在Eclipse中手动添加src / it / java文件夹:

Project Properties -> Java Build Path -> Source tab -> Add Folder . 项目属性 -> Java构建路径 -> 源选项卡 -> 添加文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM