Home > database >  How to run test in gradle based on condition on Spring Boot application
How to run test in gradle based on condition on Spring Boot application

Time:08-08

I am using Spring Boot application with gradle. Currently there are 9 tests will be executed during build and test gradle task. One test is excluded as it will affect the other tests. So I have excluded the SampleTest as below in build.gradle

test {
    useJUnitPlatform()

    filter {
        excludeTestsMatching "com.test.func.SampleTest"
    }

}

Now I want to run all the tests in the following order. My SampleTest should be run at end of all tests. How should I achieve it? Any help would be appreciated.

  1. Other 9 tests
  2. com.test.func.SampleTest

CodePudding user response:

Need to review the tests why you need to do this.

Gradle simply delegates execution to the JUnit runner.

So if you want specific test class ordering, you will need to create a Suite (with @SelectClasses:) and specify the test classes in the order you want

You can fellow this doc to create test suite https://howtoprogram.xyz/2016/08/16/junit-5-test-suite/

If all the tests are in the same test file, you could use @Order with junit5 to give the order exécution for each test: https://www.baeldung.com/junit-5-test-order

CodePudding user response:

There is an option based on your question, so you can create additional task for testing SampleTest run.

task testLast(type: Test) {
    include '**/SampleTest.*'
}

test {
    exclude '**/SampleTest.*'
}

test.dependsOn testLast
  • Related