Ran into this issue as well. Works fine from IntelliJ running a single test or all tests in a class but fails when I try to build with gradle (e.g. gradlew clean build).
Found that setting forkEvery=1 allows the build to complete successfully. This is a work around to having some leaky tests. (https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test:forkEvery)
test {
forkEvery = 1
}
For debugging why it’s happening, I’ve setup a junit test suite to run multiple test classes in a row from within IntelliJ. https://github.com/junit-team/junit4/wiki/Aggregating-tests-in-suites