如何使用maven在spring boot中仅执行集成测试

时间:2018-03-10 15:05:56

标签: maven spring-boot junit4 maven-failsafe-plugin

我试图通过maven命令分离单元和集成测试。

的pom.xml

....

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Fast*</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

这是我的集成测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = StudentApplication.class,
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class StudentControllerIT {

...

这是单元测试

@RunWith(SpringRunner.class)
@WebMvcTest(value = StudentController.class, secure = false)
public class StudentFastControllerTest {

....

现在,当我尝试运行命令mvn test时,只执行StudentFastControllerTest次测试,但是当我运行命令mvn integration-testmvn verify时,两个测试类都被执行而不是{ {1}}。

1 个答案:

答案 0 :(得分:1)

编辑:有两种方法对我有用:

1)使用maven-failsafe配置(我从this answer获得了帮助):

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
            <execution>
                <id>it-tests</id>
                <phase>none</phase> 
                <configuration>
                    <includes>
                        <include>**/*IT.java</include>
                    </includes>
                </configuration>
            </execution>
        </executions>
    </plugin>

请注意,此执行是i)用单词 it-tests 标识的,ii)阶段设置为 none 。后者将这些测试从默认生命周期中分离出来,而前者使我们能够使用以下命令按需运行它们:

mvn failsafe:integration-test@it-tests

在集成测试后缀IT也是很重要的,我们将其包含在此<includes>部分中的方式与对其他插件的<exclude>部分相同例如surefire。


2)使用配置文件

个人资料部分中,为集成测试添加个人资料:

            <profile>
            <id>it-tests</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/*IT.java</include>
                            </includes>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

另外,将以下内容添加到配置文件配置部分如果,您将集成测试放在单独的测试源目录中,在本例中为test-integration

<testSourceDirectory>test/test-integration/java</testSourceDirectory>

现在回到插件部分,然后在您的maven-surefire-plugin上,通过将以下内容添加到mvn test的配置部分中来排除集成测试:

<excludes>
    <exclude>**/*IT.java</exclude>
</excludes>

最后,将以下执行添加到您的maven-failsafe-plugin:

<executions>
    <execution>
        <goals>
            <goal>integration-test</goal>
            <goal>verify</goal>
        </goals>
    </execution>
</executions>

因此(至少在我的情况下),集成测试不是在mvn test期间执行,而是在我运行时执行:

mvn failsafe:integration-test -Pit-tests

仅执行集成测试。

我希望这也对您有用。