蚂蚁无法识别@ParametrizedTest和@CsvSource

时间:2019-01-31 06:34:24

标签: eclipse ant junit5

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

我已将Eclipse指向单独安装在系统中的最新版本的Ant,并且还添加了junit-jupiter官方Ant page中提到的所有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

您能指导我设置正确的东西吗?

谢谢

1 个答案:

答案 0 :(得分:1)

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

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}.jar相同的方式添加junit-jupiter-api-${version}.jar