3

On a springboot application I have unit and integration tests. What I want is to control which group of tests to run and when. I mean running units OR integration tests, BUT not both.

I know it is possible through maven, but I was wondering if Spring Profile can be used to achieve this. I mean something like, marking unit tests in one profile, and integration tests in another profile. And at run time I supply a profile, which triggers only running those tests that belongs to that profile.

Jahid Shohel
  • 1,395
  • 4
  • 18
  • 34

2 Answers2

0

You could achieve the desired behavior with the following addition to build.gradle:

test {
    useJUnitPlatform()
    exclude "**/*IT*", "**/*IntTest*"
    testLogging {
        events 'FAILED', 'SKIPPED'
    }
}

task integrationTest(type: Test) {
    useJUnitPlatform()
    description = "Execute integration tests."
    group = "verification"
    include "**/*IT*", "**/*IntTest*"
    testLogging {
        events 'FAILED', 'SKIPPED'
    }
}
check.dependsOn integrationTest

This will create test and integrationTest under verification task. Now you can run either one or the other or both by running the check task. The integration tests must have IntTest or IT in the class name.

Also, don't forget to add the following dependencies:

dependencies {
    testImplementation "org.junit.jupiter:junit-jupiter-engine:5.3.2"
    testImplementation "junit:junit:4.12"
}
Shmarkus
  • 178
  • 1
  • 11
-1

Yes you can group these two kinds of tests by Spring profile.
You will have to create an application.properties specific to unit tests and another one specific to integration tests if you want to run one or the other.
For example application-ut.properties and application-it.properties with each one their specificities.

Then you should specify @ActiveProfiles for each test class according to their nature.

For integration test classes, for example :

@ActiveProfiles("it")
public class FooIntegrationTest {}

@ActiveProfiles("it")
public class BarIntegrationTest {}

For unit test classes, for example :

@ActiveProfiles("ut")
public class FooTest {}

@ActiveProfiles("ut")
public class BarTest {}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • I tried it, and doesn't work. I have created a profile named "e2e" and a properties file named "application-e2e.properties" (which is literally empty). And then I tried with "mvn clean install -Dspring.profiles.active=e2e". But its running all tests. – Jahid Shohel Jan 26 '18 at 09:35
  • 2
    ActiveProfiles declares only which profiles should be activated when this test is being run. And not what profile needs to be active for this test to run. https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/ActiveProfiles.html – Rüdiger Schulz Jun 07 '18 at 07:52