Maven / Spring boot project - how to skip @SpringBootTest

Viewed 15238

In my project there are many tests marked with @SpringBootTest which I don't regard as unit tests and rather as integration tests. So I would like to run only the unit tests when I execute:

mvn clean install

actually I want to run this command as part of pre-commit git hook but @SpringBootTest makes it longer to finish execution.

Is there a way to exclude the tests marked with @SpringBootTest? May be there is a pattern we can pass to maven that excludes/certain tests. Or may be write a test suite that includes the spring boot tests.

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.

@Update2: I have a solution that looks promising:

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.

3 Answers

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.

Examples:

If you're on JUnit 4, use @IfProfileValue annotation on the test class or method.

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.

Example:

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

See the docs for more details.

Related