使用Spring Maven上下文,我想要基于Maven配置文件运行特定的测试。我希望有一种简单的方法来标记测试组。如果可能的话,我想要使用注解。有哪些选项可供选择,例如Maven命令行参数、Maven配置文件规范等。
假设我有以下测试:
示例:
// annotation("integration")
public class GeopointFormatterTest {
@Test
public void testIntegration1() { ... }
@Test
public void testIntegration2() { ... }
像 @Profile(用于创建bean)和 @ActiveProfile(用于选择创建bean所需的特定配置文件)这样的注释,当然不能用于选择测试。所有测试只会运行类似以下语句的声明:
mvn clean install -Pdevelopment
mvn clean install -Pdevelopment -Dspring.profiles.active=acceptance
mvn clean install -Pdevelopment -Dspring.profiles.active=integration
如建议的那样,我还使用了 @IfProfileValue。这是一种基于系统属性值选择测试的好方法。可以通过 CustomProfileValueSource 类覆盖系统属性值,例如:@ProfileValueSourceConfiguration(CustomProfileValueSource.class)
编辑和替代方案
下面的出色答案集中于 JUnit 的 @Category 机制。感谢所有人!
另一种方法是通过以下步骤进行:[1] 在 maven 配置文件中设置属性,[2] 使用该属性通过标准 surefire 测试插件中的 <excludeGroups> 跳过测试。
[1] 通过配置文件设置属性:
<profiles>
<profile>
<id>integrationtests</id>
<properties>
<integration.skip>false</integration.skip>
<acceptance.skip>true</acceptance.skip>
</properties>
</profile>
... other profiles
[2] 使用surefire测试插件中的属性来跳过测试。
<build>
<plugins>
<plugin>
<!-- Run the integration test-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<skipTests>${acceptance.skip}</skipTests>
开始使用 Maven: mvn clean install –Pintegrationtests