Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle doesn't run tests

In my Gradle project I have a very simple JUnit test:

import org.junit.jupiter.api.Test
import static org.junit.jupiter.api.Assertions.fail

class ApiCallerTest {

    @Test
    void testSetApiKey() {
        fail();
    }
}

When I run the test in IntelliJ, I get a list of tasks with the message that the build was successful. Eg.:

Testing started at 19:52 ...
> Task :cleanTest UP-TO-DATE
> Task :compileJava UP-TO-DATE
> Task :compileGroovy NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :compileTestGroovy NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
BUILD SUCCESSFUL in 0s

This should not happen since I am expecting the test to fail. I also get a message "Test events were not received".

This is my gradle.build file:

plugins {
    id 'groovy'
    id 'java'
}

group 'com.ipdive'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')
}

According to suggestions on the internet I tried adding this code to the gradle.build file.

test {
    useJUnitPlatform()
}

But that didn't help.

I also tried changing the Gradle setting to "Run tests using" -> "IntelliJ IDEA". When I run the test after this change, I get an error message:

Internal Error occurred.
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
    at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:189)
    at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:168)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.junit.platform.commons.JUnitException: MethodSelector [className = 'ApiCallerTest', methodName = 'testSetApiKey', methodParameterTypes = ''] resolution failed
    at org.junit.platform.launcher.listeners.discovery.AbortOnFailureLauncherDiscoveryListener.selectorProcessed(AbortOnFailureLauncherDiscoveryListener.java:39)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:102)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.run(EngineDiscoveryRequestResolution.java:82)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolver.resolve(EngineDiscoveryRequestResolver.java:113)
    at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:45)
    at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:69)
    at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:181)
    ... 6 more
Caused by: org.junit.platform.commons.PreconditionViolationException: Could not load class with name: ApiCallerTest
...
...

Can you please tell me what to do so I can run my unit-tests with Gradle?


EDIT: Added screenshot of the project structure.

Project structure

like image 836
Jan Horčička Avatar asked Feb 17 '20 19:02

Jan Horčička


2 Answers

You can use the configuration from the official junit5-samples. There you can find the junit5-jupiter-starter-gradle-groovy project that uses the following configuration:

plugins {
    id 'groovy'
    id 'eclipse' // optional (to generate Eclipse project files)
    id 'idea' // optional (to generate IntelliJ IDEA project files)
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(localGroovy())
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

It works with Gradle 6.0.1, Java 8 and 11.

In order to run the tests you can use the following command: gradle test or configure in your IDE as depicted:

strong text

You will see something like:

Testing started at 17:34 ...
> Task :cleanTest
> Task :compileJava NO-SOURCE
> Task :compileGroovy UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :compileTestGroovy UP-TO-DATE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass$3$1 (file:/C:/Users/jmoreno/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-all/2.3.11/f6b34997d04c1538ce451d3955298f46fdb4dbd4/groovy-all-2.3.11.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass$3$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
com.example.project.CalculatorTestsJava > addsTwoNumbers() PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[1] PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[2] PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[3] PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[4] PASSED
com.example.project.CalculatorTests > add(int, int, int)[1] PASSED
com.example.project.CalculatorTests > add(int, int, int)[2] PASSED
com.example.project.CalculatorTests > add(int, int, int)[3] PASSED
com.example.project.CalculatorTests > add(int, int, int)[4] PASSED
com.example.project.CalculatorTests > 1 + 1 = 2() PASSED
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 3s
5 actionable tasks: 2 executed, 3 up-to-date
17:34:08: Tasks execution finished ':cleanTest :test --tests *'.

Another point to take into account is the location of the classes. According to documentation when we use the groovy plugin we can mix java & groovy files only in the groovy folder, in this case, the groovy class is in the java folder where we can only put java files.

It's important to use the correct dependencies too if you declare different versions of groovy it could generate compilation errors.

like image 105
JuanMoreno Avatar answered Oct 22 '22 19:10

JuanMoreno


The test file was (mistakenly) created as a Groovy file. You can tell by the missing semicolons at the end of the import statements or by the almost square file icon in the IntelliJ.

Therefore it should be in the /test/groovy folder. That's why it wasn't picking up the test file in the /test/java folder because there was no java file. Once moved to the correct folder, Gradle picked it up.

Later there was also a problem with the Groovy version. These two statements were clashing with each other:

compile 'org.codehaus.groovy:groovy-all:2.3.11'
implementation(localGroovy())

Only one of them should be used.

like image 26
Jan Horčička Avatar answered Oct 22 '22 19:10

Jan Horčička