Search code examples
javagradlejunitjunit4junit5

Gradle Can't Find JUnit Platform


I've recently upgraded to JUnit 5, and I'm trying to maintain a JUnit 4 suite via junit-jupiter-vintage using the EXACT INSTRUCTIONS provided just about everywhere you can find a sample.

Note, I'm using an offline build, because I must, so while I can download any Jar I need, I'm not actively connected to any maven repository.

Gradle version is 4.9, the -all.zip distribution, not package installed but unzipped and added to the PATH variable (not that it should matter in the slightest).

apply plugin: 'java'

repositories{
  flatDir dirs: 'file:$rootDir/lib'
}
test{
  useJUnitPlatform()
}

dependencies{
  testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.0'
  testCompileOnly 'junit:junit:4.12'
  testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.0'
  testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.5.1'
}

The output is a disappointing:

NoClassDefFoundError: org/junit/platform/engine/support/hierarchical/HierarchicalTestEngine

These are files found in the JUnit Platform jars, and serve as the launching engine for JUnit 5. Gradle 4.6 and higher supposedly supports this engine natively, and I can see jars in

gradle-4.9/lib/plugins/
  ...
  junit-platform-commons-1.0.3.jar
  junit-platform-engine-1.0.3.jar
  junit-platform-launcher-1.0.3.jar
  ...

Manually adding the latest platform as a testRuntimeOnly dependency seems to allow my JUnit 5 test files to work, but it fails to detect any of my JUnit4 tests. It also feels like an obvious hack, since every sample build file I can locate doesn't need to do that. I'm hopeful that if I can resolve the gradle issue, I will also resolve my JUnit4 issue.

Why would my (offline) gradle build be unable to locate the main test runner for JUnit5?


Solution

  • Gradle is trying to use Maven Repositories to resolve transitive dependencies, and since this installation is offline, there's no way for it to do so. All of the JUnit transitive dependencies need to be on the runtime classpath, which in this case means manually including them.

    I'm going to use Maven to figure this out and download them all on this internet-connected machine and move the local repository it creates onto the system that needs it.