繁体   English   中英

Maven/Spring 启动项目——如何跳过@SpringBootTest

[英]Maven / Spring boot project - how to skip @SpringBootTest

在我的项目中有许多标有 @SpringBootTest 的测试,我不认为它们是单元测试,而是集成测试。 所以我想在执行时只运行单元测试:

mvn 全新安装

实际上,我想将此命令作为 pre-commit git hook 的一部分运行,但 @SpringBootTest 使完成执行的时间更长。

有没有办法排除标有@SpringBootTest 的测试? 可能有一种模式我们可以传递给 maven 排除/某些测试。 或者可以编写一个包含 Spring Boot 测试的测试套件。

我做了谷歌搜索来实现上述目标,但运气不佳。

还有更好的方法吗?

@更新:约束是无法修改 maven pom 文件。

@Update2:我有一个看起来很有希望的解决方案:

1. Use @Category("IntegrationTests") for @SpringBootTests tests.
2. Create TestSuite with excludeCategory:
@RunWith(CategoryRunner.class)
@ExcludeCategory("IntegrationTests")
public class TestSuite {
}
3. From mvn command line, run only TestSuite.

我不确定这是最好的。 欣赏任何人的更好方法。

如果您有不同类型的测试,并且希望能够指定要运行的测试,您可以使用@Conditionals 或@Profile 来实现。

例子:

如果您使用的是 JUnit 4,请在测试类或方法上使用@IfProfileValue注释。

例子:

@IfProfileValue(name ="spring.profiles.active", value ="IntegrationTests")

如果您使用的是 JUnit 5(此时您应该使用),请使用@EnabledIf@DisabledIf

例子:

@DisabledIf(
    expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
    reason = "Disabled on Mac OS"
)

有关更多详细信息,请参阅文档

尝试

mvn clean install -DskipTests 

或者

mvn clean install -Dmaven.test.skip=true

有关更多选项,请参阅以下链接

https://mkyong.com/maven/how-to-skip-maven-unit-test/

https://www.baeldung.com/maven-skiping-tests

暂无
暂无

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

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