Issue
I try to run this single unit test of this open-source project on GitHub with IntelliJ.
Execution failed for task ':test'.
No tests found for given includes: [DNAnalyzer.MainTest.mainClassshouldExist](--tests filter)
The test class
package DNAnalyzer;
import org.junit.jupiter.api.Test;
public class MainTest {
@Test
public void mainClassshouldExist() throws ClassNotFoundException {
Class.forName("DNAnalyzer.Main");
}
}
The build.gradle
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/7.5.1/userguide/building_java_projects.html
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit test framework.
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.9.1'
// This dependency is used by the application.
implementation "com.google.guava:guava:31.0.1-jre"
// Picocli
implementation "info.picocli:picocli:4.6.3"
}
application {
// Define the main class for the application.
mainClass = "DNAnalyzer.Main"
}
test {
useJUnitPlatform()
}
jar {
manifest {
attributes 'Main-Class': 'DNAnalyzer.Main'
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
What I have tried
Updating to the newest JUnit
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.9.1'
Adding this to my build.gradle file:
test { useJUnitPlatform() }
The only workaround yet:
When I go to Settings -> Build-Tool -> Gradle and set the Test execution to "Run tests wit: IntelliJ IDEA" the tests are executed correctly.
Thanks
Solution
Problem is coming from the naming of the package which contains your test classes, DNAnalyzer
, which does not really follow java naming conventions as it's starts with/contains capital letters.
When executing single test case or test class, IntelliJ will delegate to Gradle by invoking test
task with a specific filter, as follows
./gradlew :test --tests "DNAnalyzer.MainTest.mainClassshouldExist"
And in Gradle there is this issue : https://github.com/gradle/gradle/issues/20350 : "test --tests does not work with packages which contain capital letters".
Try to move your test classes in another well-named package and you won't have this issue.
Answered By - M.Ricciuti
Answer Checked By - Katrina (JavaFixing Volunteer)