ant 不识别@ParametrizedTest 和@CsvSource

ant does not recognize @ParametrizedTest and @CsvSource

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

我已经将 eclipse 指向我系统中单独安装的最新版本的 Ant,并添加了官方 Ant page for junit-jupiter

中提到的所有 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

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

谢谢

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

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}.jar复制到ANT_HOME/lib
  • 或添加 junit-jupiter-params-${version}.jarjunit-jupiter-api-${version}.jar
  • 相同