kkotula
kkotula

Reputation: 177

Gradle cannot run tests both in java and groovy

I'm facing an issue I cannot solve. I have following code structure in my application:

src
    - main
        - java
    - test
        - java
        - groovy

It's rather conventional. Some tests are written in java using JUnit5 and some are written in groovy using Spock. When I run gradlew test only tests in /src/test/groovy are run. There are no errors. Java test are simply not being run. I added on purpose failing test to /src/test/java to check whether there's an issue with printing reports. The test is not failed, because whole package /src/test/java is ignored somehow. I tried to find solution on SO but none of the answeres helped me. I messed around with ideas like:

sourceSets {
  test {
    java { srcDirs = "src/test/java" }
    groovy { srcDirs = "src/test/groovy" }
  }
}

That didnt help. Here are some snippets from my build.gradle

Plugin:

apply plugin: 'groovy'

Test dependencies:

testImplementation("org.springframework.boot:spring-boot-starter-test:2.1.4.RELEASE") {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation("org.junit.jupiter:junit-jupiter:5.5.2")
testImplementation(group: 'org.spockframework', name: 'spock-core', version: '1.3-groovy-2.5')
testImplementation(group: 'org.spockframework', name: 'spock-spring', version: '1.3-groovy-2.5')
testImplementation(group: 'org.codehaus.groovy', name: 'groovy-all', version: '1.3-groovy-2.5', ext: 'pom')

Test task configuration:

test {
    useJUnit()
    failFast = true
    testLogging.showStandardStreams = true

    reports {
        junitXml.enabled = true
    }
    finalizedBy jacocoTestReport
    finalizedBy jacocoTestCoverageVerification
    finalizedBy check
    testLogging {
        events "PASSED", "STARTED", "FAILED", "SKIPPED"
    }
}

Do you have any ideas?

Upvotes: 3

Views: 3543

Answers (2)

le cuong
le cuong

Reputation: 11

I have following configuration:

testImplementation 'org.spockframework:spock-core:2.0-M1-groovy-2.5'
testImplementation 'org.spockframework:spock-junit4:2.0-M1-groovy-2.5'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
testImplementation 'junit:junit:4+'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.6.2'

And yes, in test task, I've used useJUnitPlatform()

test {
        useJUnitPlatform()
        afterTest { desc, result ->
            def executionTime = result.endTime - result.startTime
            println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType} in ${executionTime} ms"
        }
        testLogging {
            showStandardStreams = true
        }
    }

Do not have to configure engine for junitTestPlatform My configuration works for junit 4 tests + spock tests. (have no time not to upgrade -> junit 5). I have junit 4 rules in my tests and they work properly with this configuration

Documentation of junit 5 is very helpful: https://junit.org/junit5/docs/current/user-guide/#running-tests-build

Upvotes: 1

kkotula
kkotula

Reputation: 177

Ok, I fixed this issue and just right after it I faced another one. I'm posting this answer for future me or some fellow developers in troubles.

First of all I had to change useJUnit() to useJUnitPlatform() as suggested by Mike Hill.

That solved the first issue with java test not being run. The new problem that arose - junit tests were running extremely slow and some of them got randomly stuck. Every time I ran tests, one of them just froze and stayed that way for couple of minutes. After that time OutOfMemory was thrown. I do not know why! When running those test from IJ, everything was running smooth. I had to add this to build.gradle to fix the issue.

Test dependencies:

testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'

Also add this to test closure:

test {
    useJUnitPlatform() {
            includeEngines 'junit-vintage'
            includeEngines 'junit-jupiter'
    }
}

I do not claim that this is proper solution. It was more of a guess after scrolling through the documentation. I would not recommend copying it without further reading on differences between useJUnit() and useJUnitPlatform(). For me it did the trick and I didn't have more time do the research.

Upvotes: 6

Related Questions