参数化测试:JUnit 未读取集合?
我正在使用 JUnit 4 参数化测试注释对来自未知数量文件的数据执行相等性测试。
由于某些奇怪的原因,测试总是检查相同的文件,就好像始终使用用于传递参数的集合中的相同数组一样。
我确信我在这里遗漏了一些基本的东西,但我看不出是什么。有人可以为我提供有用的提示吗?
这是我的代码:
@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 用于调试:静态参数生成器中的那个显示正确文件的名称,这意味着对象“数据”是正确的。测试本身总是打印相同的内容,这意味着测试总是使用相同的参数数组运行(但它运行了 7 次,这是我集合中的数组数量!)我不明白。关于传递给 JUnit 的参数类型,我是否遗漏了一些内容?
I am using JUnit 4 parametrized test annotations to perform equality tests on data coming from a unknown number of files.
For some weird reason, the test always checks the same file, as if the same Array was always used from the Collection used to pass the parameters.
I'm sure I'm missing something elementary here, but I can't see what. Could someone provide me with a helpful tip ?
Here is my code :
@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
}
}
It's partially obfuscated but there should be everything needed to find my error...
The System.out.prints are used for debug : the one in the static parameter-generator show the names of the correct files, meaning that the object "data" is correct. The one in the test itself always prints the same thing, meaning that the test is always run with the same array of parameters (But it runs 7 times, the number of arrays I have in my collection !) I don't get it. Is there something I have missed about the type of the parameters to pass to JUnit ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个
String[] xmlArgs = new String[3];
应该在 for 循环中。
您总是更新 xmlArgs-Array 的同一实例并插入它。
This
String[] xmlArgs = new String[3];
should be in the for-loop.
Your are always updating the same instance of the xmlArgs-Array and inserting it..