当前位置: 首页 > 知识库问答 >
问题:

ClassNotFoundException试图在IntelliJ中使用Maven运行JUnit测试

亢仰岳
2023-03-14

我正在IntelliJ中用基本的JUnit5测试处理一个Kotlin项目,使用Maven管理依赖项。我已经通过IntelliJrun'classname'命令成功地执行了JUnit测试。

今天早些时候,我尝试开始将测试分组到测试套件中,并在pom.xml中添加了junit-platform-runner和junit-platform-commons作为依赖项。从那以后,我每次尝试执行任何测试时都得到以下ClassNotFoundException。

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/commons/util/ClassNamePatternFilterUtils
    at org.junit.platform.launcher.core.LauncherFactory.loadAndFilterTestExecutionListeners(LauncherFactory.java:122)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:108)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:75)
    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:48)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:31)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:128)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.util.ClassNamePatternFilterUtils
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 12 more
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.millibyte1</groupId>
    <artifactId>cubesearch</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.14</java.version>
        <kotlin.version>1.3.72</kotlin.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <!--<dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>1.7.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.7.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.7.0-M1</version>
            <type>pom</type>
        </dependency>-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>default-testCompile</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>java-compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>java-test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>kotlin-maven-plugin</artifactId>
                <groupId>org.jetbrains.kotlin</groupId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                                <sourceDir>${project.basedir}/src/main/java</sourceDir>
                            </sourceDirs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                                <sourceDir>${project.basedir}/src/test/java</sourceDir>
                            </sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>ClassNotFoundException trying to run JUnit tests using Maven in IntelliJ - Stack Overflow

感谢任何帮助,提前致谢!

共有1个答案

田成仁
2023-03-14

删除我的/home/username/.m2目录并重新运行MVN clean Install解决了这个问题。我不知道为什么MVN dependency:purge-local-repository没有。

 类似资料:
  • 问题内容: 我刚刚在Eclipse上第一次安装了插件m2e。 我编写了一个简单的JUnit(版本4)测试。我可以从Eclipse中运行它,但不能从pom.xml中运行它(alt单击,运行方式,Maven测试)。我想我需要告诉Maven搜索该课程,但我只是不知道如何。 另外,我在groupId“ junit”中找不到JUnit 4:只有版本3.8.1可用。我真的需要为3.x版本而不是4+版本编写测试

  • 问题内容: 我有大量的测试,大约需要半小时才能运行,并且希望能够并行进行测试。 有没有办法用IntelliJ IDEA 9做到这一点? 问题答案: IDEA仅从版本10开始才了解并行JUnit测试。 有一个跟踪器问题,您可以投票并等待进度:http: //youtrack.jetbrains.net/issue/IDEA-47103 我们计划将其添加到IDEA 10中,但是优先级将取决于票数。

  • 我已经编写了一个JUnit测试套件,用于运行多个测试用例。 现在我想一次运行我的测试套件类(AllTest.java),以便所有测试都由一个类触发、执行和管理。我知道maven故障保护插件是可用的,但是有没有其他更简单的方法从maven调用JUnit测试套件呢? 我不想使用另一个插件。 这是我当前的maven-故障安全-插件配置:

  • 以下是我的目标: 为版本4.1.6.release中的Spring代码编写Jupiter测试用例[Junit5中的测试用例] 编写Jupiter测试用例[Junit5中的测试用例] 使用Junit4运行程序[在IntelliJ IDE]运行 IDE和代码版本详细信息: Spring版本:4.1.6.release Java版本:1.8[Java 8] IDE:IntelliJ Community

  • 我正在尝试用Maven/Intellij运行spock测试。但是maven和intellij都没有接受测试类。它肯定会拾取类,但不会在类中执行任何测试。 2)Surefire插件配置正确,因为它会拾取文件进行测试 3)target/test-classs文件夹中生成的测试类 我需要帮助我在这里错过了什么。