Junit5 参数化测试尝试时,Java 异常未找到匹配的测试

标签 java junit5 parameterized-tests

所以我尝试使用 JUnit5 中的 ParameterizedTest 并设置 pom 以加载所有内容,现在有以下测试类。

    import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import com.tdameritrade.ctg.cashmovement.enums.transaction.TransactionProcessEnum;
import com.tdameritrade.ctg.cashmovement.enums.transaction.TransactionStatusEnum;
import com.tdameritrade.ctg.junit.categories.UnitTest;

/**
 * Test class for Posting Common Utils
 */
@RunWith(JUnitPlatform.class)
@Category(UnitTest.class)
public class PostingCommonUtilsTest {

    private static Stream<Arguments> dataSetForTransactionStatus() {
        return Stream.of(Arguments.of(TransactionStatusEnum.ERRORED.getName(), true),
                         Arguments.of(TransactionStatusEnum.COMPLETED.getName(), false));
    }

    /**
     * Test method for
     * {@link com.tdameritrade.ctg.cashmovement.posting.PostingCommonUtils#checkTransactionErrored(java.lang.String)}.
     */
    @ParameterizedTest
    @MethodSource("dataSetForTransactionStatus")
    public void testCheckTransactionErrored(final String status, final boolean result) throws Exception {
        assertThat(PostingCommonUtils.checkTransactionErrored(status), is(equalTo(result)));
    }
}

我收到以下错误:

java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testCheckTransactionErrored], {ExactMatcher:fDisplayName=testCheckTransactionErrored(com.tdameritrade.ctg.cashmovement.posting.PostingCommonUtilsTest)], {LeadingIdentifierMatcher:fClassName=com.tdameritrade.ctg.cashmovement.posting.PostingCommonUtilsTest,fLeadingIdentifier=testCheckTransactionErrored]] from org.junit.internal.requests.ClassRequest@654f0d9c
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68)
    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:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

也许我错过了它,但我已经尝试搜索此错误,并且我发现的所有内容都指向已经设置的内容。比如有一篇文章说pom不在一起是对的,但是我有一样的东西。

这是我的 pom.xmls:

父 POM:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><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>
...  
      <properties>
...
        <junit.version>4.12</junit.version>
        <junit.platform.version>1.3.2</junit.platform.version>
        <junit.jupiter.version>5.3.2</junit.jupiter.version>
    </properties>

...
</project>

子 POM:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><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>

    <parent>
        <groupId>com.tdameritrade.ctg</groupId>
        <artifactId>cashmovement-parent</artifactId>
        <version>1-SNAPSHOT</version>
        <relativePath>../cashmovement-parent</relativePath>
    </parent>

    <name>CTG Cash Movement Server</name>
    <groupId>com.tdameritrade.ctg</groupId>
    <artifactId>cashmovement-server</artifactId>
    <packaging>war</packaging>

    <dependencies>
...
        <dependency>
            <artifactId>junit-addons</artifactId>
            <groupId>junit-addons</groupId>
            <version>${junit-addons.version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>xerces</groupId>
                    <artifactId>xmlParserAPIs</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>xerces</groupId>
                    <artifactId>xercesImpl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
...
        <plugins>
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <forkCount>${surefire.fork.count}</forkCount>
                    <reuseForks>${surefire.fork.reuse}</reuseForks>
                    <groups>${maven.surefire.include.groups}</groups>
                    <excludedGroups>${maven.surefire.exclude.groups}</excludedGroups>
                    <properties>
                        <includeTags>junit5</includeTags>
                    </properties>                    
                </configuration>
...
            </plugin>
        </plugins>
    </build>
</project>

怎么了?

如果我通过命令行运行,这是错误的堆栈跟踪:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project cashmovement-common: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project cashmovement-common: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    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:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
    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:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoFailureException: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute (AbstractSurefireMojo.java:579)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    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:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
    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:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)

最佳答案

从您的堆栈跟踪来看,您似乎正在尝试使用 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader 启动 JUnit 5 测试。这不太可能奏效,同样的问题as reported in this question .

尝试使用 Maven 从命令行运行测试,如 Running a Single Test 中所述文档:

mvn -Dtest=PostingCommonUtilsTest test

如果测试成功运行,或者您看到与 Eclipse 中完全相同的异常,那么说明您的 Eclipse 设置是正确的,这是框架的问题。否则,您的 Eclipse 设置存在缺陷并且不支持 JUnit 5。

这可能是 Eclipse 版本的问题。据官方Embracing JUnit 5 with Eclipse页:

We do not support running tests in a setup where an old Eclipse build (not having JUnit 5 support) is using a new Eclipse build (having JUnit 5 support) as target. Also, developers who have the JDT JUnit runtime bundles (org.eclipse.jdt.junit.runtime, org.eclipse.jdt.junit4.runtime) checked out and pull the latest changes will run into the above issue. You are expected to use a new Eclipse build for the development.

关于Junit5 参数化测试尝试时,Java 异常未找到匹配的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53639725/

相关文章:

java - 更改 Android 上的小数点分隔符

java - Lombok @Synchronized 与 Mockito 抛出 NPE

java - 测试方法返回特定对象junit

robotframework - Robot Framework 是否支持 Gherkin 数据表?

java - 下载所有依赖 Java 库(递归)

java - 帮助将字节的字符串数组类型转换为实际字节

java - Android SimpleDateFormat 解析错误

Junit5:预期嵌套异常

selenium-webdriver - 如何使用 NodeJS - Protractor 从 Excel 读取/写入 Excel?