使用JUnit5时,我收到一条警告:“未找到类异常:排除原因消耗过滤器”

cotxawn7  于 2022-12-13  发布在  其他
关注(0)|答案(2)|浏览(138)

我正在尝试使用JUnit5。
首先,我向maven项目添加了依赖项:

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.3.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

然后,我创建了一个测试:

package com.example.multicurrency;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class JunitTests {
    @Test
    void testAssertTrue() {
        assertTrue(false);
    }
}

然后,我运行测试,得到的结果如下:

org.junit.platform.launcher.core.DefaultLauncher handleThrowable
warning: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoClassDefFoundError: org/junit/platform/engine/support/filter/ExclusionReasonConsumingFilter
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.support.filter.ExclusionReasonConsumingFilter

org.opentest4j.AssertionFailedError: 
Expected :<true> 
Actual   :<false>

结果如我所料。但是警告令我困惑。警告是什么意思?

iq0todco

iq0todco1#

目前使用junit-jupiter-engine5.3.x版本与Surefire 2.22.2配合使用。

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.3.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

看起来你被https://issues.apache.org/jira/browse/SUREFIRE-1564震惊了,它描述了一个已知的问题,即Surefire 2.22.0内部保留在版本1.2.0上,用于所有junit-platfrom-xyz工件。
您还应该为Surefire 3.x提供一个try - afaik,它与所有版本的JUnit都兼容:https://maven.apache.org/surefire/maven-surefire-plugin/

uyto3xhc

uyto3xhc2#

1.当您添加JUNIT 5依赖项时,它会自动包含“junit-platform-engine”和“junit-jupiter-API”。您可以在“Pom.xml”的依赖项层次结构中检查这一点。这样您就可以删除随JUNIT 5沿着添加的其他依赖项。仅JUNIT 5依赖项就足够了。
1.同时确保没有引用JUNIT 4依赖项。如果您尝试混合使用JUNIT和JUNT 5,则可能会出现此类警告,因为您正在欺骗JVM。

相关问题