简体   繁体   English

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

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

In my project there are many tests marked with @SpringBootTest which I don't regard as unit tests and rather as integration tests.在我的项目中有许多标有 @SpringBootTest 的测试,我不认为它们是单元测试,而是集成测试。 So I would like to run only the unit tests when I execute:所以我想在执行时只运行单元测试:

mvn clean install mvn 全新安装

actually I want to run this command as part of pre-commit git hook but @SpringBootTest makes it longer to finish execution.实际上,我想将此命令作为 pre-commit git hook 的一部分运行,但 @SpringBootTest 使完成执行的时间更长。

Is there a way to exclude the tests marked with @SpringBootTest?有没有办法排除标有@SpringBootTest 的测试? May be there is a pattern we can pass to maven that excludes/certain tests.可能有一种模式我们可以传递给 maven 排除/某些测试。 Or may be write a test suite that includes the spring boot tests.或者可以编写一个包含 Spring Boot 测试的测试套件。

I did google search to achieve the above but don't have much luck.我做了谷歌搜索来实现上述目标,但运气不佳。

Is there even a better way?还有更好的方法吗?

@Update: Constraint is maven pom file can't be modified. @更新:约束是无法修改 maven pom 文件。

@Update2: I have a solution that looks promising: @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.

I am not sure this is the best.我不确定这是最好的。 Appreciate anyone's better approach.欣赏任何人的更好方法。

If you have different kinds of tests, and want to be able to specify which tests to run, you can do that with @Conditionals or with @Profile.如果您有不同类型的测试,并且希望能够指定要运行的测试,您可以使用@Conditionals 或@Profile 来实现。

Examples:例子:

If you're on JUnit 4, use @IfProfileValue annotation on the test class or method.如果您使用的是 JUnit 4,请在测试类或方法上使用@IfProfileValue注释。

Example:例子:

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

If you're on JUnit 5, as you should be by this time, use @EnabledIf or @DisabledIf .如果您使用的是 JUnit 5(此时您应该使用),请使用@EnabledIf@DisabledIf

Example:例子:

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

See the docs for more details.有关更多详细信息,请参阅文档

try either尝试

mvn clean install -DskipTests 

or或者

mvn clean install -Dmaven.test.skip=true

For more options refer to below links有关更多选项,请参阅以下链接

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

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

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

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