Eclipse Buildship: Getting JUnit 5 running in Eclipse

I created a new eclipse modular java project.
src/main/java
src/test/java
Created JUnit 5 test case, which it added the Eclipse JUnit 5 library.
Running the test worked fine.
I then converted it to a gradle project. Created a build.gradle, removed the project from Eclipse and imported it again using Gradle Buildship. Now the JUnit test case will not run.

Class not found no.smeaworks.mytest.MyTest
java.lang.ClassNotFoundException: com.company.mytest.MyTest
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:773)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:502)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

I am not quite sure what I am missing from my gradle setup.

Steps:

  1. Created the project

Screenshot_20190224_154218

Screenshot_20190224_154257

Screenshot_20190224_154328

  1. Created the JUnit test case

Screenshot_20190224_154503

  1. Running the test case successfully failed test

  1. Converted it to Gradle

     import java.time.format.DateTimeFormatter
     import java.time.ZoneId
     import org.apache.tools.ant.filters.ReplaceTokens
    
     plugins {
         id 'java'
         id 'eclipse'
         id 'application'
         id 'org.openjfx.javafxplugin' version '0.0.7'
     }
    
     apply(from: rootProject.file('eclipse.gradle'))
    
     mainClassName = "$moduleName/com.company.mytest.MyTestApplication"
    
     repositories {
         mavenCentral()
     }
    
     javafx {
         version = "11.0.+"
     
         modules = [
             'javafx.graphics',
             'javafx.controls',
             'javafx.media',
             'javafx.web',
             'javafx.fxml'
         ]
     }
    
     final def junitJupiterGroup = 'org.junit.jupiter'
     final def junitJupiterVersion = '5.4.0'
    
     final def junitPlatformGroup = 'org.junit.platform'
     final def junitPlatformVersion = '1.4.0'
     dependencies {
         testImplementation(group: junitJupiterGroup, name: 'junit-jupiter-api', version: junitJupiterVersion)
         testRuntimeOnly(group: junitJupiterGroup, name: 'junit-jupiter-engine', version: junitJupiterVersion)
         testRuntimeOnly(group: junitJupiterGroup, name: 'junit-jupiter-params', version: junitJupiterVersion)
    
         testRuntimeOnly(group: junitPlatformGroup, name: 'junit-platform-launcher', version: junitPlatformVersion)
         testRuntimeOnly(group: junitPlatformGroup, name: 'junit-platform-runner', version: junitPlatformVersion)
     }
    
     jar {
         doFirst {
             manifest {
                 attributes(
                     'Implementation-Title': project.name,
                     'Implementation-Version': project.version,
                     'Implementation-Vendor': project.group,
                     'Specification-Title': project.name,
                     'Specification-Version': project.version,
                     'Specification-Vendor': project.group,
                     "Main-Class": mainClassName,
                     'Built-By': System.getProperty('user.name'),
                     'Created-By': System.properties['java.version'] + " (" + System.properties['java.vendor'] + " " + System.properties['java.vm.version'] + ")",
                     'Permissions': 'all-permissions',
                     'Codebase': '*'
                 )
             }
         }
     }
    
     test {
         useJUnitPlatform()
    
         reports {
             html.enabled = true
         }
     }
    
  2. Deleted the project and Import with Gradle Buildship

Screenshot_20190224_160354

The additional eclipse.gradle
Without this, then Eclipse cannot resolve the JUnit dependencies that buildship puts on the classpath.

eclipse {
    project {
        natures 'org.eclipse.buildship.core.gradleprojectnature'
    }

    classpath {
        file {
            whenMerged {
                entries.findAll { isModule(it) }.each { //(1)
                    it.entryAttributes['module'] = 'true'
                }

                entries.findAll { isSource(it) && isTestScope(it) }.each {
                    it.entryAttributes['test'] = 'true'
                }

                entries.findAll { isLibrary(it) && isTestScope(it) }.each {
                    it.entryAttributes['test'] = 'true'
                }
            }
        }

        defaultOutputDir = file('build')
        downloadSources = true
        downloadJavadoc = true
    }
}

boolean isLibrary(entry) { return entry.properties.kind.equals('lib') }
boolean isTestScope(entry) { return entry.entryAttributes.get('gradle_used_by_scope').equals('test'); }
boolean isModule(entry) { isLibrary(entry) && !isTestScope(entry); }
boolean isSource(entry) { return entry.properties.kind.equals('src'); }

This is the JUnit Eclipse run configuration

Screenshot_20190224_194856

This is the output from “Show Command Line”

/usr/java/jdk-11/bin/java -ea --add-opens com.company.mytest/com.company.mytest=ALL-UNNAMED --add-modules=ALL-MODULE-PATH -Dfile.encoding=UTF-8 -p /home/sverre/workspace2/my-test/bin/main:/home/sverre/workspace2/my-test/bin/test -classpath /home/sverre/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-web/11.0.2/49ca03e2fd58cbb0ad94f6a6bd11e0c043e541a7/javafx-web-11.0.2-linux.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-media/11.0.2/8bc5daaa19f2ff103cd85f751ad021eb71ba7db7/javafx-media-11.0.2-linux.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-media/11.0.2/30b3246c67f308de4d7aefbf99acef8d1dce32ad/javafx-media-11.0.2.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-fxml/11.0.2/1b8a331d5f393b48de0aef59b3c967a312967290/javafx-fxml-11.0.2-linux.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-controls/11.0.2/5f6929050a744aab39ebfc1e8e5dd03bcd2ad47b/javafx-controls-11.0.2-linux.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-controls/11.0.2/4ab633cf1eea60f76e2ae9905aedac862da88b08/javafx-controls-11.0.2.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-graphics/11.0.2/ef4c34f0ca77cd99100b76c2ccf1dce383572bb1/javafx-graphics-11.0.2-linux.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-graphics/11.0.2/e522eb4ea422eceeee207b1c266ba3db19b2343a/javafx-graphics-11.0.2.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-base/11.0.2/8db178adb1085d455e5ef643a48c34959c8771c1/javafx-base-11.0.2-linux.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-base/11.0.2/7fb2e4a8528ec9e434a9ac9ee98b39af79e6dcb8/javafx-base-11.0.2.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.junit.jupiter/junit-jupiter-api/5.4.0/1a09f25a160f71c267f9ebe3b229b17805c683e9/junit-jupiter-api-5.4.0.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.junit.platform/junit-platform-commons/1.4.0/e0b2ed8fac32ad6469d75d034e759f1969db8dda/junit-platform-commons-1.4.0.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.apiguardian/apiguardian-api/1.0.0/3ef5276905e36f4d8055fe3cb0bdcc7503ffc85d/apiguardian-api-1.0.0.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.opentest4j/opentest4j/1.1.1/efd9f971e91074491ea55b19f67b13470cf4fcdd/opentest4j-1.1.1.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.junit.jupiter/junit-jupiter-engine/5.4.0/30083adf3fbd7b734f12e6cb4d99dedab99fc34b/junit-jupiter-engine-5.4.0.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.junit.jupiter/junit-jupiter-params/5.4.0/7edc0a25bd3318d05a213aee09441d72e0921fab/junit-jupiter-params-5.4.0.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.junit.platform/junit-platform-runner/1.4.0/a8a0ef915bf648671d9404c1cfe52e4b80b14243/junit-platform-runner-1.4.0.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.junit.platform/junit-platform-launcher/1.4.0/5cc8b7b305ffc79aa124a1a4fe5efab114f28cac/junit-platform-launcher-1.4.0.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.junit.platform/junit-platform-engine/1.4.0/b4419dc190a5d82b9cac6b52e0af9140895c3f87/junit-platform-engine-1.4.0.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.junit.platform/junit-platform-suite-api/1.4.0/8c03ee0615180d219c2dac86b004bd5d32b5649c/junit-platform-suite-api-1.4.0.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/2973d150c0dc1fefe998f834810d68f278ea58ec/junit-4.12.jar:/home/sverre/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar:/home/sverre/eclipse/configuration/org.eclipse.osgi/399/0/.cp:/home/sverre/eclipse/configuration/org.eclipse.osgi/397/0/.cp org.eclipse.jdt.internal.junit.runner.RemoteTestRunner -version 3 -port 35471 -testLoaderClass org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader -loaderpluginname org.eclipse.jdt.junit5.runtime -classNames com.company.mytest.MyTest

Hey @DJViking.

I presume you’ve already found an answer since you first asked your original question eight months ago. But while I was searching for something else, I happened to stumble across this old post of yours. It looked like a good real-world functional test to exercise an upcoming release of my mrJar plugin.

My ultimate goal in this exercise was pretty much exactly what your title says. I wanted to test out how mrJar handles running a JUnit 5 test — and a JavaFX application — from a Gradle project imported into Eclipse…

Looking at the build.gradle in your post, that project has the JUnit 5 modules declared in its dependencies{} block. Buildship will automatically do what’s necessary regarding those dependencies. For those reasons, I skipped your original steps where you added the JUnit 5 library. I don’t think that step is neccessary if the ultimate goal is mainly to run a test in the IDE.

Also, Buildship gives me a bunch of prebuilt Run Configurations. It presents them in a neat collection in the form of the Gradle Tasks view. It even gives me the Ctrl + Alt, X, G hotkey combo to run whichever test’s currently in the editor. So for that reason, I don’t go through the trouble of an extra user-created run configuration. Aside from the built-in Gradle Tasks being simpler and more convenient anyway, an extra run configuration would be redundant — in my opinion.

I’m hoping you can share the solution you eventually settled on. TIA :+1: