Writing JUnit tests, testLogging events "no candidate found"

I’ve wrote some simple tests for my Java app which I can run from the IDE using the gutter button, but if I use gradle test no output is logged.

D:\Coding\slotcar\battleship>gradle test

> Configure project :
Hello

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.3/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s

There is an error with the events argument (I’m not familiar with Groovy, method?), on hover it says “No candidates found”. Could this be the problem?
image

The rest of the build.gradle file

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'junit:junit:4.12'
    implementation 'junit:junit:4.12'
    compile 'org.junit.jupiter:junit-jupiter:5.6.2'
}

I’m expecting some output like this

> Task :test
CalculatorTests > testMultiplication() PASSED
CalculatorTests > Add 1 and 2, result should be 3 PASSED
CalculatorTests > testDivisionByZero() PASSED
CalculatorTests > testDivision() PASSED

Your problem is, probably more that you don’t have any test running?
The test event logging works fine here.

Actually your gradle script looks pretty suspicous, you have two time JUnit 4.12 in there and you don’t even seem to intend to use it but want to use JUnit 5.
Furthermore you have put JUnit 5 in the compile configuration which is deprecated sind years and shouldn’t be used, if some site or guide told you to use it, report a bug against it.
And you also put both JUnit 4 and JUnit 5 to the production class path, not the test class path, so I wonder how your tests even compile, unless your tests also reside in the main source set which would then also explain why no tests are executed and thus no test events logged.

1 Like

Thanks for your reply.

Actually your gradle script looks pretty suspicous, you have two time JUnit 4.12 in there and you don’t even seem to intend to use it but want to use JUnit 5.

I believe this must have been InteliJ auto importing JUnit 4 to the classpath when I added the annotation @Test the first time, but you’re right and I’ve removed the JUnit4 dependencies now.

Furthermore you have put JUnit 5 in the compile configuration which is deprecated sind years and shouldn’t be used, if some site or guide told you to use it, report a bug against it.

I’m not sure how where I copied this code from, but I think it must be an older tutorial, thanks, I’ve corrected it.

And you also put both JUnit 4 and JUnit 5 to the production class path, not the test class path,

I’m not sure what you mean? I’ve set up my project directory structure as I see it in most of the tutorials, with IDEA Gradle is building and running, and running tests. Here is my project structure, the test class is in the folder marked as test sources root.
image

Ok, that is actually a very uncommon setup, I don’t know what tutorials you read. :smiley:

So just to repeat, the setting works fine and as expected for me. Are you sure your tests actually run? Can you provide an MCVE?

@SiAust If what you’ve shown is your entire build.gradle, then Gradle will not see any of your source code with that directory layout. The java plugin creates two default source sets, main and test. By default main expects your application code to be in <project_dir>/src/main/java and test expects your test code to be in <project_dir>/src/test/java.

So, as Vampire suggests, Gradle is not running any tests because Gradle has not been configured with your specific directory layout. I suggest changing your directory structure to match the default Gradle conventions, but you could instead reconfigure the source sets. See the Java Plugin docs for more information.

I suspect that this project might not be linked to Gradle in IntelliJ but is instead defining it’s own modules and dependencies. @SiAust If this is true, then I recommend importing/linking the Gradle project. That will help keep Gradle and IntelliJ in sync.