首页 > 解决方案 > ant 无法识别 @ParametrizedTest 和 @CsvSource

问题描述

尽管添加了所需的依赖项,但我的 ant(版本 1.10.5)构建无法编译 junit5 测试

我已经将eclipse指向我系统中单独安装的最新版本的Ant,并且还添加了junit-jupiter官方Ant页面中提到的所有jar文件

来自ant-junit-samples页面的构建脚本如下:

     <?xml version="1.0" encoding="UTF-8"?>
        <project name="junit5-jupiter-starter-ant" default="build" basedir=".">

    <fail message="Ant 1.10.5 is required!">
        <condition>
            <not>
                <antversion atleast="1.10.5"/>
            </not>
        </condition>
    </fail>

    <path id="test.classpath">
        <pathelement path="build/test"/>
        <pathelement path="build/main"/>
        <fileset dir="${ant.home}/lib" includes="*.jar" />
    </path>

    <target name="build" description="clean build" depends="clean, test" />

    <target name="clean">
        <delete dir="build"/>
        <echo message="${ant.home}" />    </target>

    <target name="init">
        <mkdir dir="build/main"/>
        <mkdir dir="build/test"/>
        <mkdir dir="build/test-report"/>
    </target>

    <target name="compile" depends="init">
        <javac destdir="build/main" srcdir="src/main/java" includeantruntime="false"/>
        <javac destdir="build/test" classpathref="test.classpath" srcdir="src/test/java" includeantruntime="false"/>
    </target>

    <!-- https://junit.org/junit5/docs/snapshot/user-guide/#running-tests-build-ant -->
    <target name="test.junit.launcher" depends="compile">
        <junitlauncher haltOnFailure="true" printSummary="true">
            <classpath refid="test.classpath"/>
            <testclasses outputdir="build/test-report">
                <fileset dir="build/test">
                    <include name="**/*Tests.class"/>
                </fileset>
                <listener type="legacy-xml" sendSysOut="true" sendSysErr="true"/>
                <listener type="legacy-plain" sendSysOut="true" />
            </testclasses>
        </junitlauncher>
    </target>

    <!-- https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher -->
    <target name="test.console.launcher" depends="compile">
        <java classpathref="test.classpath" classname="org.junit.platform.console.ConsoleLauncher" fork="true" failonerror="true">
            <arg value="--scan-classpath"/>
            <arg line="--reports-dir build/test-report"/>
        </java>
        <junitreport todir="build/test-report">
            <fileset dir="build/test-report">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="build/test-report/html"/>
        </junitreport>
    </target>

    <target name="test" depends="test.junit.launcher, test.console.launcher" />

</project>

但是我遇到了错误

[javac] import org.junit.jupiter.params.ParameterizedTest;
    [javac]                                ^
     org.junit.jupiter.params.provider does not exist
    [javac] import org.junit.jupiter.params.provider.CsvSource;
    [javac]                                         ^
    [javac] CalculatorTests.java:31: error: cannot find symbol
    [javac]     @ParameterizedTest(name = "{0} + {1} = {2}")
    [javac]      ^
    [javac]   symbol:   class ParameterizedTest
    [javac]   location: class CalculatorTests
    [javac]  \CalculatorTests.java:32: error: cannot find symbol
    [javac]     @CsvSource({
    [javac]      ^
    [javac]   symbol:   class CsvSource
    [javac]   location: class CalculatorTests
    [javac] 4 errors

您能否指导我需要正确设置的内容?

谢谢

标签: eclipseantjunit5

解决方案


junit5 -jupiter-starter-ant示例将 JUnit 5 的“独立”JAR 复制到$ANT_HOME/lib文件夹中。这个准备好的 Ant 安装包括 JUnit 5 必须提供的所有包(Platform、Jupiter、Vintage),并将它们传递给通过或其他方式引用ant 运行时的任务:class-path

wget --directory-prefix "${ant_folder}/lib" "...junit-platform-console-standalone-1.3.2.jar"

详见build.shhttps ://github.com/junit-team/junit5-samples/blob/master/junit5-jupiter-starter-ant/build.sh

因此,您可以:

  • 复制junit-platform-console-standalone-${version}.jarANT_HOME/lib
  • junit-jupiter-params-${version}.jar以与您相同的方式添加junit-jupiter-api-${version}.jar

推荐阅读