Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit4.11不显示参数值,但显示索引_Java_Eclipse_Ant_Junit - Fatal编程技术网

Java JUnit4.11不显示参数值,但显示索引

Java JUnit4.11不显示参数值,但显示索引,java,eclipse,ant,junit,Java,Eclipse,Ant,Junit,我们有一个包含许多JUnit测试类的项目,直到最近才开始使用Eclipse内部JUnit实现。 为了从ant构建脚本运行我们的测试,我们更改了项目的构建路径,以使用外部junit-4.11.jar和所需的hamcrest核心库。 我们的一些测试类在@Parameters注释中使用参数化JUnit Runner和(name=“{0}”)选项。当使用Eclipse内置JUnit运行这些测试类时,输出显示第一个参数的值,而不仅仅是索引。 现在,在使用外部JUnit之后,无论我是从Eclipse内部运行

我们有一个包含许多JUnit测试类的项目,直到最近才开始使用Eclipse内部JUnit实现。 为了从ant构建脚本运行我们的测试,我们更改了项目的构建路径,以使用外部junit-4.11.jar和所需的hamcrest核心库。 我们的一些测试类在@Parameters注释中使用参数化JUnit Runner和(name=“{0}”)选项。当使用Eclipse内置JUnit运行这些测试类时,输出显示第一个参数的值,而不仅仅是索引。 现在,在使用外部JUnit之后,无论我是从Eclipse内部运行测试,还是从带有“test”目标的ant构建脚本运行测试,都只显示索引。 下面是一个测试类:

package foo;

import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ParameterTest {

    public ParameterTest(String _name, String _value)
    {
        name = _name;
        value = _value;
    }   

    @Parameters(name = "{0}")
    public static Iterable<String[]> testData() {
        return Arrays.asList(new String[][] {
                { "name1", "value1" },
                { "name2", "value2" } });
    }

    @Parameter(0)
    public String name;

    @Parameter(1)
    public String value;

    @Test
    public void test() {
        System.out.println(name+value);
    }
}
package-foo;
导入java.util.array;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.junit.runners.Parameterized;
导入org.junit.runners.Parameterized.Parameter;
导入org.junit.runners.Parameterized.Parameters;
@RunWith(参数化的.class)
公共类参数测试{
公共参数测试(字符串_名称,字符串_值)
{
名称=_名称;
值=_值;
}   
@参数(name=“{0}”)
公共静态Iterable testData(){
返回Arrays.asList(新字符串[][]{
{“name1”,“value1”},
{“名称2”,“值2”});
}
@参数(0)
公共字符串名称;
@参数(1)
公共字符串值;
@试验
公开无效测试(){
System.out.println(名称+值);
}
}
为了从ant脚本运行测试,必须添加构造函数,否则会导致IllegalArgumentException。 可以删除@Parameter(x)注释,但这不会改变结果

编辑: 如果我从Eclipse内部运行这个类(“RunAs->JUnit Test”),我会得到“initializationError”,失败跟踪显示“java.lang.Exception:Test类应该正好有一个公共零参数构造函数”。 如果我使用“test”目标从ant build脚本中运行测试,测试运行时不会出错,但输出显示“test[0]”和“test[1]”,而不是“test[name1]”和“test[name2]”

总结如下: 1.如果我向测试类中添加一个构造函数,并根据需要使用尽可能多的参数,它将不会在Eclipse中运行。 2.在没有构造函数的情况下,测试从Eclipse中运行,测试的命名正确地取自配置的参数。但是,如果没有构造函数,测试将无法从ant脚本运行

目的是在Eclipse和ant脚本中运行测试,并在这两种场景中显示正确的名称

编辑2:
根据文档,您可以使用@Parameter注入参数,也可以使用构造函数。当我编辑上面的测试类并删除@Parameter注释时,它在Eclipse中运行良好,并在每次运行旁边显示正确的名称。但是,当从ant脚本运行时,它仍然正常运行,但不显示名称,而是显示测试旁边的索引位置。

重要的是将正确的JUnit版本(在本例中为4.11)添加到JUnit ant任务的类路径中。下面是一个编译和执行ParameterTest的简单ant文件。目录结构如下所示:

src
   /test/ParameterTest.java
lib
   /junit-4.11.jar
   /hamcrest-core-1.3.jar
build.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project name="test">
<property name="src" value="src" />
<property name="build" value="target/classes" />
<property name="lib" value="lib" />
<property name="test.result" value="target/test-results" />
<property name="java.version" value="1.8" />


<target name="init">
    <mkdir dir="target/test-results" />
    <mkdir dir="target/classes" />
</target>

<target name="compile" depends="init">
    <javac srcdir="src" destdir="${build}" listfiles="no" deprecation="off" debug="on" nowarn="on"
           includeantruntime="true" source="${java.version}" target="${java.version}">
        <classpath>
            <fileset dir="${lib}" includes="*.jar"/>
        </classpath>
    </javac>
</target>

<target name="test"  depends="compile">


<junit printsummary="on" haltonfailure="no" showoutput="no"
       fork="yes"
       forkmode="once" tempdir="/tmp">

    <formatter type="xml"/>

    <classpath>
        <!-- adding first the classpath inherited from the shell -->
        <pathelement path="${java.class.path}"/>
        <!-- add lib folder with junit-4.11.jar -->
        <fileset dir="${lib}" includes="*.jar"/>
    </classpath>

    <classpath location="${build}" />

    <batchtest todir="${test.result}">
        <!-- location of your compiled Junit classes -->
        <fileset dir="${src}">
            <include name="**/*Test*.java" />
        </fileset>
    </batchtest>
</junit>
</target>

使用ant test执行后,您可以查看target/test results/test-test.ParameterTest.xml,并查看以下行:

  <testcase classname="test.ParameterTest" name="test[name1]" time="0.0"/>
  <testcase classname="test.ParameterTest" name="test[name2]" time="0.0" />


您能否澄清运行该测试类时发生的情况与预期的情况?以及
@参数是什么?它没有在嵌套类中列出,在of
Parameterized
中这里描述了@Parameter:该类在JUnit内容中是错误的。您可以使用无构造函数和@Parameter(x)注释,或者使用带有参数名称和无参数注释的构造函数。这在Eclipse和IntelliJ中有效。当您在ant执行方面遇到问题时,请发布构建脚本以及如何构建ant类路径。我想这就是问题所在。使用ant脚本进行了测试,它在xml文件中生成了以下行``因此它似乎在ant中工作。我能够从您的build.xml中修复我的build.xml。似乎与类路径顺序有关。