当前位置: 首页 > 面试题库 >

JUnit 5测试用例未执行

羊舌胡非
2023-03-14
问题内容

我在NetBeans 8.2中的一个新的干净Maven项目中创建了此测试:

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

public class JUnit501Test {

    @Test
    void firstTest() {
        fail("epicly");
    }
}

如果我运行测试,日志显示

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running JUnit501Test
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

我的pom.xml包含以下依赖项:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.1</version>
    <scope>test</scope>
</dependency>

是什么导致JUnit 5忽略测试方法?

更新:我添加了surefire插件,这是完整的pom.xml:

<?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.timeshuttle</groupId>
    <artifactId>Junit5Test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.1</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.1</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

输出更改为:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results:

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 1.672s
Finished at: Mon Oct 16 16:19:04 CEST 2017
Final Memory: 6M/15M
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.20.1:test (default-cli) on project Junit5Test: There are test failures.

Please refer to F:\Sandbox_Java\justin\Junit5Test\target\surefire-reports for the individual test results.
Please refer to dump files (if any exist) [date]-jvmRun[N].dump, [date].dumpstream and [date]-jvmRun[N].dumpstream.
There was an error in the forked process
org.apache.maven.surefire.report.RunListener.testSetStarting(Lorg/apache/maven/surefire/report/ReportEntry;)V
org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
org.apache.maven.surefire.report.RunListener.testSetStarting(Lorg/apache/maven/surefire/report/ReportEntry;)V
at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:673)
at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:535)
at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:280)
at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:245)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1124)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:954)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:832)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
-> [Help 1]

并且surefire-reports文件夹包含两个“ .dump”文件。第一个内容:

# Created on 2017-10-16T16:17:32.959
java.lang.NoSuchMethodError: org.apache.maven.surefire.report.RunListener.testSetStarting(Lorg/apache/maven/surefire/report/ReportEntry;)V
    at org.junit.platform.surefire.provider.JUnitPlatformProvider.invokeSingleClass(JUnitPlatformProvider.java:137)
    at org.junit.platform.surefire.provider.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:126)
    at org.junit.platform.surefire.provider.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:105)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)

问题答案:

确保将您maven-surefire-plugin的配置正确地设置为:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version> <!-- Specific due to memory leak in 2.20 -->
    <dependencies>
         <!--Custom provider and engine for Junit 5 to surefire-->
         <dependency>
             <groupId>org.junit.platform</groupId>
             <artifactId>junit-platform-surefire-provider</artifactId>
             <version>1.0.1</version>
         </dependency>
         <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter-engine</artifactId>
             <version>5.0.1</version>
         </dependency>
     </dependencies>
     <configuration>
         <argLine>${argLine}</argLine>
     </configuration>
</plugin>

而且您不需要引擎作为依赖项,因此可以从<dependencies>标签中删除它:-

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

注意 :Junit5的有关使用maven运行测试的官方文档指定使用surefire
2.19.1,因为该插件的2.20发行版中发生内存泄漏。



 类似资料:
  • 我刚刚通过composer安装了PHPUnit,试图通过跳上测试驱动开发的潮流,成为一名优秀的后端开发人员。然后我运行命令vendor/bin/phpunit,可以看到phpunit安装正确。 以下是我的文件结构: 在phpunit内部。xml文件我有以下内容: 如果我运行vendor/bin/phpunit,我可以看到我的配置文件已加载,但没有运行任何测试。 我基本上在tests文件夹和扩展的P

  • 右键点击Run可以很好地工作。我用 Intellijidea 2017.1.5, Gradle, JunitPlatformVersion='1.0.0-M6', JunitJupiterVersion='5.0.0-M6'

  • Maven执行 问题-这个自定义提供程序是否不再受支持,或者是否有任何解决方案可以使用、和/或执行测试? 注意-测试执行在JUnit-4中运行良好。

  • 问题内容: Maven执行 我正在尝试使用我的一个Maven项目,但在此阶段无法使用- 我得到的输出是- 尝试实施上述解决方案@ Surefire没有采用Junit5测试来将依赖关系更新为- 并将插件更新为- 但输出保持不变。 问题 -这是自定义提供没有更多的支持,或者是有什么解决方案使用执行测试,和/或 现在? 注意 -测试执行在JUnit-4上运行良好。 问题答案: 您应该像这样配置maven

  • 我想知道如何执行一个文件或实体来理解我现在要运行指定的JUnit5案例列表? 我知道JUnit5中的一些类似于测试套件的新功能即将出现,但它们显然现在还没有出现,期望在不久的将来出现可能过于乐观了。我现在已经积累了几个测试,接下来还有几个,必须独立地启动每个测试越来越乏味,随着测试数量的增加,这种情况只会变得更糟。今天我是否可以编写代码来自动一次运行多个测试?这将使我渡过难关,直到即将到来的代码可

  • 我试图定义一个,如示例MockitoExtension所提供的,但无法成功地使用参数化的类实例。 期待着关于如何用在中实现接口的类的参数化实例测试接口的建议。