我正在尝试通过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-test
或mvn verify
时,两个测试类都被执行,而不仅仅是StudentControllerIT
。
这对我有用:
在个人资料部分中,为集成测试添加个人资料:
<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>
另外,将以下内容添加到配置文件配置部分if,您将集成测试放在一个单独的测试源目录中,在本例中为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
仅执行我的集成测试。
我希望这也对您有用。