4

JUnit 5ライブラリをインストールして、例を実行しようとしました。

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
public class myClass {
    @Test
    void myMethod() {
       System.out.println("myMethod");
    }
}

しかし、 initializationErrorを得ました:

java.lang.NoClassDefFoundError: org/junit/platform/engine/discovery/ClasspathRootSelector
    at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:48)
    at org.junit.jupiter.engine.JupiterTestEngine.resolveDiscoveryRequest(JupiterTestEngine.java:50)
    at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:43)
    at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:108)
    at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:78)
    at org.junit.platform.runner.JUnitPlatform.generateTestTree(JUnitPlatform.java:113)
    at org.junit.platform.runner.JUnitPlatform.<init>(JUnitPlatform.java:96)
    at org.junit.platform.runner.JUnitPlatform.<init>(JUnitPlatform.java:88)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.discovery.ClasspathRootSelector
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 25 more
4

1 に答える 1

0

Right-click Project -> Build Path -> Add Libraries -> JUnit -> (version i.e. 5) -> Finish.

(また)

Right-click Project -> Build Path -> Configure Build Path -> Libraries Tab -> Add Library -> JUnit -> (version i.e. 5) -> Finish -> Apply & Close.

これは、Eclipse を使用していることを前提としています。他の IDE には、おそらく同様の手順があります。JUnitがPOMにあったときに同じことを見ましたが、ライブラリの依存関係はありませんでした。

于 2019-09-05T16:33:25.357 に答える