7

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.

user1539343
  • 1,569
  • 6
  • 28
  • 45

3 Answers3

5

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:

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • This strategy will need support from spring. While I am trying to run unit tests from maven clean install. So I am not sure whether your strategy will apply in my case. – user1539343 Mar 05 '20 at 13:03
  • @user1539343 Your entire question is about `@SpringBootTest`, so this is Spring, and therefore "support from spring" is a guarantee. Don't see what the problem is with that. – Andreas Mar 05 '20 at 15:53
  • The problem I see is it trying to invoke spring IOC to do some bean creation etc. That really defeats the purpose of fast loading tests that is the gist of this question. The goal is to run the plain junits without any dependencies - all dependencies mocked. And while running tests, whatever test runner is trying to run the tests should simply ignore tests annotated with @SpringBootTest. – user1539343 Mar 05 '20 at 17:34
  • `ConditionalOnProperty` is for beans, not for tests. – Abhijit Sarkar Aug 30 '20 at 11:41
4

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.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
0

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://www.baeldung.com/maven-skipping-tests

RamPrakash
  • 1,687
  • 3
  • 20
  • 25
  • 1
    This is not related to the question. – user1539343 Mar 04 '20 at 19:36
  • @user1539343 Do you want to skip all unit tests or specific ones? Easy way to stop running junit tests through maven is to skip tests. this might solve the confusion https://stackoverflow.com/questions/9123075/maven-how-can-i-skip-test-in-some-projects-via-command-line-options – RamPrakash Mar 04 '20 at 20:04
  • "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:" – user1539343 Mar 04 '20 at 22:15