admin管理员组

文章数量:1122846

I've created a Java Gradle project, but, for a reason, the tests for it have to be in Kotlin. Let's take this test, for example:

package org.***.***

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

class TestTest {
    @Test
    fun testThat2Plus3Is5() {
        Assertions.assertEquals(5, 2 + 3)
    }
}

But when I run the tests using ./gradlew test, they don't seem to run at all. It says that 'Test events were not received'. And when I run this test using IntelliJ IDEA, it fails with the following error:

Internal Error occurred.
org.junit.platformmons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:160)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverSafely(EngineDiscoveryOrchestrator.java:134)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:108)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:80)
    at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:110)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.junit.platformmons.JUnitException: ClassSelector [className = 'org.fedmam.msqueue.TestTest'] 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:46)
    at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:69)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:152)
    ... 13 more
Caused by: org.junit.platformmons.PreconditionViolationException: Could not load class with name: org.fedmam.msqueue.TestTest
    at org.junit.platform.engine.discovery.ClassSelector.lambda$getJavaClass$0(ClassSelector.java:75)
    at org.junit.platformmons.function.Try$Failure.getOrThrow(Try.java:335)
    at org.junit.platform.engine.discovery.ClassSelector.getJavaClass(ClassSelector.java:74)
    at org.junit.jupiter.engine.discovery.ClassSelectorResolver.resolve(ClassSelectorResolver.java:66)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.lambda$resolve$2(EngineDiscoveryRequestResolution.java:134)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
    at java.base/java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1602)
    at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129)
    at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
    at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:647)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:185)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:125)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:91)
    ... 18 more
Caused by: java.lang.ClassNotFoundException: org.fedmam.msqueue.TestTest
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
    at org.junit.platformmons.util.ReflectionUtils.lambda$tryToLoadClass$9(ReflectionUtils.java:829)
    at org.junit.platformmons.function.Try.lambda$call$0(Try.java:57)
    at org.junit.platformmons.function.Try.of(Try.java:93)
    at org.junit.platformmons.function.Try.call(Try.java:57)
    at org.junit.platformmons.util.ReflectionUtils.tryToLoadClass(ReflectionUtils.java:792)
    at org.junit.platformmons.util.ReflectionUtils.tryToLoadClass(ReflectionUtils.java:748)
    ... 33 more

Process finished with exit code 254

Here's how build.gradle looks:

plugins {
    id 'java'
}

group 'org.***'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    testImplementation 'org.jetbrains.kotlinx:lincheck:2.34'
}

test {
    useJUnitPlatform()
}

Here's how the project structure looks:

- src
  - main
    - java
      - org.***.***
        - ...
  - test
    - java  // no matter if I rename it to java or kotlin
      - org.***.***
        - ...
        - TestTest.kt

I've created a Java Gradle project, but, for a reason, the tests for it have to be in Kotlin. Let's take this test, for example:

package org.***.***

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

class TestTest {
    @Test
    fun testThat2Plus3Is5() {
        Assertions.assertEquals(5, 2 + 3)
    }
}

But when I run the tests using ./gradlew test, they don't seem to run at all. It says that 'Test events were not received'. And when I run this test using IntelliJ IDEA, it fails with the following error:

Internal Error occurred.
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:160)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverSafely(EngineDiscoveryOrchestrator.java:134)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:108)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:80)
    at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:110)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.junit.platform.commons.JUnitException: ClassSelector [className = 'org.fedmam.msqueue.TestTest'] 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:46)
    at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:69)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:152)
    ... 13 more
Caused by: org.junit.platform.commons.PreconditionViolationException: Could not load class with name: org.fedmam.msqueue.TestTest
    at org.junit.platform.engine.discovery.ClassSelector.lambda$getJavaClass$0(ClassSelector.java:75)
    at org.junit.platform.commons.function.Try$Failure.getOrThrow(Try.java:335)
    at org.junit.platform.engine.discovery.ClassSelector.getJavaClass(ClassSelector.java:74)
    at org.junit.jupiter.engine.discovery.ClassSelectorResolver.resolve(ClassSelectorResolver.java:66)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.lambda$resolve$2(EngineDiscoveryRequestResolution.java:134)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
    at java.base/java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1602)
    at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129)
    at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
    at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:647)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:185)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:125)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:91)
    ... 18 more
Caused by: java.lang.ClassNotFoundException: org.fedmam.msqueue.TestTest
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
    at org.junit.platform.commons.util.ReflectionUtils.lambda$tryToLoadClass$9(ReflectionUtils.java:829)
    at org.junit.platform.commons.function.Try.lambda$call$0(Try.java:57)
    at org.junit.platform.commons.function.Try.of(Try.java:93)
    at org.junit.platform.commons.function.Try.call(Try.java:57)
    at org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(ReflectionUtils.java:792)
    at org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(ReflectionUtils.java:748)
    ... 33 more

Process finished with exit code 254

Here's how build.gradle looks:

plugins {
    id 'java'
}

group 'org.***'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    testImplementation 'org.jetbrains.kotlinx:lincheck:2.34'
}

test {
    useJUnitPlatform()
}

Here's how the project structure looks:

- src
  - main
    - java
      - org.***.***
        - ...
  - test
    - java  // no matter if I rename it to java or kotlin
      - org.***.***
        - ...
        - TestTest.kt
Share Improve this question asked Nov 22, 2024 at 23:38 FedMamFedMam 131 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

The setup looks good, but you're not going to get anywhere with Kotlin code unless you have something to compile it – and Java is not going to do that for you.

To do this you should add the Kotlin plugin to the project:

plugins {
    id "org.jetbrains.kotlin.jvm" version "2.0.21"
}

Then your test should run.

A couple of other points that you may like to know:

  • Kotlin files can sit in the java directory by default but putting them there is unconventional
  • The Kotlin JVM plugin applies the Java plugin so you do not also need to apply the Java plugin (although, you can keep it in the plugins block for clarity if you prefer, since the same plugin cannot be applied twice).

本文标签: Cannot run Kotlin tests in a Java projectStack Overflow