Maven / Spring boot project - how to skip @SpringBootTest

11,956

Solution 1

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:

Solution 2

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.

Share:
11,956

Related videos on Youtube

user1539343
Author by

user1539343

Updated on June 04, 2022

Comments

  • user1539343
    user1539343 about 2 years

    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.

    • khmarbaise
      khmarbaise over 4 years
      Naming convention as mentioned and using maven-failsafe-plugin for running integration tests. And by definition SpringBootTests are integration tests.
    • matbrgz
      matbrgz over 4 years
      Move your integration tests in a separate module you only run when explicitly asking to.
  • user1539343
    user1539343 over 4 years
    This is not related to the question.
  • RamPrakash
    RamPrakash over 4 years
    @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 stackoverflow.com/questions/9123075/…
  • user1539343
    user1539343 over 4 years
    "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
    user1539343 over 4 years
    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.
  • Andreas
    Andreas over 4 years
    @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.
  • user1539343
    user1539343 over 4 years
    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.
  • Abhijit Sarkar
    Abhijit Sarkar almost 4 years
    ConditionalOnProperty is for beans, not for tests.