SureFireMaven插件不执行其中一个测试

qzlgjiam  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(469)

我有一个有四个包的项目。他们三个,一切都很好。我已经生成了junit测试,它们在构建过程中或者在运行mvntest命令时执行。
但是我的第四个包,只包含一个类。我已经为它创建了一个junit测试,但是当我运行构建时,surefire没有看到这个测试类。所有其他八个测试类都被执行,但是这个类被忽略。
当我试图使用
mvn-dtest=com.company.dpt.prj.pkg.myspecificclasstest测试
我收到了这些信息

[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.058 s
[INFO] Finished at: 2020-12-19T23:58:01-08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project my-project-name: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

我逐行检查了测试代码,找不到导致这种行为的任何原因。
我甚至删除了测试文件并重新生成了一个非常基本的文件。它仍然没有被执行。
有没有人遇到过类似的事情?任何提示都将不胜感激,因为我找不到任何线索。我正在使用JUnit4.11

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
        <version>4.11</version>
    </dependency>

所有测试最初都是使用netbeansv.12“工具->创建/更新测试”生成的,然后经过编辑以反映方法的功能。然而,除了这个测试之外,所有的测试在netbeansgui和cli“mvn test”中都运行良好。这个被忽略了。我要测试的方法的目的是为restapi客户机构建httpentity。以下是测试代码:

/**
 * Test of prepareRequest method, of class BatchServiceClient.
 * @throws java.net.URISyntaxException
 */
@Test
public void testPrepareRequest() throws URISyntaxException {
    System.out.println("prepareRequest");
    ObjectMapper mapper = Json.mapper();
    URL configFileUrl = this.getClass().getClassLoader().getResource("config_f.json");
    if (configFileUrl == null) {
        fail("Missing configuration file");
    } else {
        File configFile = new File(configFileUrl.toURI());
        BatchConfig config = BatchConfigLoader.loadConfig(configFile);
        assertNotNull(config);
        boolean validated = config.validate();
        assertTrue(validated);
        BatchServiceClient client = new BatchServiceClient(config, null, null, null, null, null, null);
        List<Object> requestData = Arrays.asList("16759408", "", "", "", "The Home Depot", "",
                "Baltimore", "MD", "", "840", "", "", "", "", "", "", "", "", "", "", 10, "",
                true, true, "merchantName", "merchantName", "merchantName");
        HttpEntity<String> request = client.prepareRequest(requestData);
        assertNotNull(request);
    }
}

我的问题是它没有在测试代码中失败。我会很高兴如果它会,因为我将能够调试它比。只是没有执行。我尝试将junit升级到v.4.13.1,将surefiremaven插件升级到v.3.0.0-m5,但没有任何影响。
我试着用-e选项运行mvn,得到了下面的堆栈跟踪,但是它仍然没有给我任何关于为什么会发生这种情况的线索。

at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:564)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)

Caused by: org.apache.maven.plugin.MojoFailureException: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)
    at org.apache.maven.plugin.surefire.SurefireHelper.reportExecution (SurefireHelper.java:149)
    at org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary (SurefirePlugin.java:364)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked (AbstractSurefireMojo.java:1041)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute (AbstractSurefireMojo.java:857)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
hgb9j2n6

hgb9j2n61#

您不能使用此模式:

-Dtest=com.company.dpt.prj.pkg.MySpecificClassTest

因为surefire和failsafe插件不支持完全限定类名。它查找文件系统,因此我向您展示了如何在配置中使用斜杠。文档正在指定此选项,请在此处再次阅读:
注意:使用语法,例如“foo/mytest.java”、“mytest.java”、“mytest”来表示“test”参数(参见includes/excludes)**
加里,你的问题是你说的是junit,但是哪一个?有两个版本4和5,更具体地说,您可以将源代码发布到我们的apachejira(或stackoverflow),然后我作为提交者可以查看您的代码。真的,没有它就无法复制,因为只有stackoverflow中的纯文本不能在我这边复制你的问题。所以,请总是附加一个链接,我们将在下次看看。上周有个人做了,非常有帮助。
关于测试注解的导入,并不是surefire插件不能识别@test import.:-)实际上,这是你的错,你没有写正确的源代码,包括pom和编译器编译的代码,因为它是即使你结合junit4和junit5在pom。然后surefire触发junit库,junit库负责识别注解,而不是surefire。如果结合使用junit4和junit5 jupiter,surefire算法更喜欢使用junit5,这就是为什么注解org.junit.test被忽略的原因,因为jupiter没有找到它和jupiter的注解。

hxzsmxv2

hxzsmxv22#

请参阅文档。
在您的情况下,命令应为:

mvn -Dtest=com/company/dpt/prj/pkg/MySpecificClassTest.java test
zzwlnbp8

zzwlnbp83#

我确实找到了问题的原因。不知何故,当ide生成(并重新生成)该测试文件时,它为@test注解放置了错误的导入。而不是

import org.junit.jupiter.api.Test;

它放置

import org.junit.Test;

因此,surefire运行的jupiter引擎无法识别@test注解。在我更正后,导入测试被执行。

相关问题