3
votes

As gradle 4.6 supports JUnit 5 with following configuration.

test {
    useJUnitPlatform()
}

It seems like the old way to change testinstance lifecycle doesn't work.

junitPlatform {
    // ...
    configurationParameter 'junit.jupiter.conditions.deactivate', '*'
    configurationParameters([
        'junit.jupiter.extensions.autodetection.enabled': 'true',
        'junit.jupiter.testinstance.lifecycle.default': 'per_class'
    ])
    // ...
}

https://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle-config-params

How to switch JUnit 5 test instance lifecycle to "per-class" in gradle 4.6+?

2

2 Answers

8
votes

Apart from using system property as described in eee's answer, junit-platform.properties file can be used. junit-platform.properties file can be put in src/test/resources.

junit-platform.properties

junit.jupiter.testinstance.lifecycle.default = per_class

Here's the answer from JUnit team. https://twitter.com/junitteam/status/970014414891094018

Either set it as a systemProperty in the task, or set it in the junit-platform.properties file. The latter is the more robust mechanism since it will be applied in the IDE as well.

4
votes

You can do this with a system property:

test {

    useJUnitPlatform {
        // ...
        systemProperty 'junit.jupiter.testinstance.lifecycle.default', 'per_class'
        // ...
    }

}