我正在使用JUnit 4参数化测试注释来对来自未知数量的文件的数据执行相等性测试。
出于某种奇怪的原因,测试总是检查同一个文件,就好像总是使用用于传递参数的Collection中的同一Array一样。
我确信我在这里遗漏了一些基本的东西,但我看不到是什么。有人能给我一个有用的建议吗?
下面是我的代码:
@RunWith(value = Parameterized.class)
public class XMLUnitTest extends XMLTestCase {
static final File ORACLE_DIR = new File("some/directory/path");
static final String PATH_TO_LOG = "some/file/path";
private String oracleXml;
private String testXml;
private String testName;
public XMLUnitTest(String o, String t, String n) {
this.oracleXml = o;
this.testXml = t;
this.testName = n;
}
// (snip) file-handling methods
@Parameters
static public Collection<Object[]> data() {
String logfile = null;
File[] xmlFilesArray;
ArrayList<String> parsedXMLs = new ArrayList<String>();
ArrayList<String> xmlNames = new ArrayList<String>();
String[] xmlArgs = new String[3];
Collection<Object[]> data = new ArrayList<Object[]>();
try {logfile = readFileAsString(PATH_TO_LOG);}
catch (IOException e) {// (snip) error handling}
String[] parsedLogs = logfile.split("someRegex", 0);
for (int i = 0; i < parsedLogs.length; i++) {
parsedLogs[i] = "<xml>"+parsedLogs[i]+"</xml>";
}
xmlFilesArray = ORACLE_DIR.listFiles();
Arrays.sort(xmlFilesArray, nameCompare);
for (int i = 0; i < xmlFilesArray.length; i++) {
if (!xmlFilesArray[i].isDirectory()) {
try {
parsedXMLs.add(convertXMLFileToString(xmlFilesArray[i].getAbsolutePath()));
xmlNames.add(xmlFilesArray[i].getName());
}
catch (Exception e) {// (snip) error handling}
}
}
for (int i = 0; i < parsedLogs.length; i++) {
xmlArgs[0] = parsedXMLs.get(i);
xmlArgs[1] = parsedLogs[i];
xmlArgs[2] = xmlNames.get(i);
System.out.println(xmlArgs[2]); //debug
data.add(xmlArgs);
}
return data;
}
@SuppressWarnings("unchecked")
@Test
public void equality() throws Exception {
System.out.println("Working on test: "+testName);
// (snip) some testing
}
}它是部分模糊的,但应该有所有需要的东西来找到我的错误...
数据用于调试:静态参数生成器中的System.out.prints显示正确文件的名称,这意味着对象"data“是正确的。测试本身总是打印相同的东西,这意味着测试总是使用相同的参数数组运行(但它运行了7次,这是我的集合中的数组数量!)我还是不明白。关于要传递给JUnit的参数类型,我是不是漏掉了什么?
发布于 2011-05-16 20:42:53
这
String[] xmlArgs = new String[3];
应该在for循环中。
您总是在更新xmlArgs-Array的同一实例并插入它。
https://stackoverflow.com/questions/6017403
复制相似问题