Java 如何为带有参数的junit测试方法使用错误收集器
我在Junit测试用例中使用ErrorCollector,目的是打印出错误,而不是停在错误的位置。 我通过使用不带参数的方法成功地尝试了ErrorCollector。但是为了减少代码重复(我写了6次相同的方法) 不带参数;因为我使用6个文件进行比较(如代码中所示), 我希望有一个通用的方法,可以用来实现打印错误和继续检查的相同目的。 当我尝试使用带有参数的方法时,我得到了异常“方法应该没有参数” 下面是我的代码Java 如何为带有参数的junit测试方法使用错误收集器,java,junit4,errorcollector,Java,Junit4,Errorcollector,我在Junit测试用例中使用ErrorCollector,目的是打印出错误,而不是停在错误的位置。 我通过使用不带参数的方法成功地尝试了ErrorCollector。但是为了减少代码重复(我写了6次相同的方法) 不带参数;因为我使用6个文件进行比较(如代码中所示), 我希望有一个通用的方法,可以用来实现打印错误和继续检查的相同目的。 当我尝试使用带有参数的方法时,我得到了异常“方法应该没有参数” 下面是我的代码 import org.gdal172.ogr.ogr;
import org.gdal172.ogr.ogr;
import org.hamcrest.CoreMatchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class DGNTester {
private ReadDGN readDgn = new ReadDGN();
private LinkedHashMap<String, Integer> layerMapCountForCompare = new LinkedHashMap<String, Integer>();
@Rule
public ErrorCollector collector = new ErrorCollector();
private File output = null;
static {
// perform OGR format registration once
if (ogr.GetDriverCount() == 0)
ogr.RegisterAll();
}
/**
* @param args
*/
public static void main(String[] args) {
DGNTester dTest = new DGNTester();
String dgnFileName_43k10 = "input\\43k10.dgn";
String dgnFileName_43k11 = "input\\43k11.dgn";
String dgnFileName_43k12 = "input\\43k12.dgn";
//The six files iam using as input.
dTest.test(dgnFileName_43k10, "dvd");
dTest.test(dgnFileName_43k10, "all");
dTest.test(dgnFileName_43k11, "dvd");
dTest.test(dgnFileName_43k11, "all");
dTest.test(dgnFileName_43k12, "dvd");
dTest.test(dgnFileName_43k12, "all");
}
@Test
public void test(String fileName, String inputType) {
System.out.println("FOR FILE -->" + fileName);
System.out
.println("---------------------------------------------------------------------------------------------------");
String fileIdentifier = fileName.substring(6, 11);
String dstFilePath = null;
String outputName = null;
if (layerMapCountForCompare != null)
layerMapCountForCompare.clear();
if (inputType.equals("dvd")) {
dstFilePath = "F:\\eclipse_helios_3.6.1_64_bit_with_jre_and_add-ons\\eclipse\\Resources\\DST\\dvd.dst";
outputName = "output\\outputfile_" + fileIdentifier
+ "_dvd.dst.txt";
}
if (inputType.equals("all")) {
dstFilePath = "F:\\eclipse_helios_3.6.1_64_bit_with_jre_and_add-ons\\eclipse\\Resources\\DST\\AllLayers.dst";
outputName = "output\\outputfile_" + fileIdentifier + ".txt";
}
layerMapCountForCompare = readDgn.getLayerFeatureCount(fileName,
dstFilePath);
// Read the text output file and Compare with the map. These are the six out put files against each input file
output = new File(outputName);
if (output.exists()) {
try {
Set keys = layerMapCountForCompare.keySet();
Iterator itr = keys.iterator();
String key = "";
Integer val;
String line;
BufferedReader br = new BufferedReader(new FileReader(output));
while ((line = br.readLine()) != null && itr.hasNext()) {
key = (String) itr.next();
val = layerMapCountForCompare.get(key);
String compare = key + "=" + val;
compare.trim();
line.trim();
//When i print this out in a positive scenario; i am able to see the values of 'compare' and 'line' as same
/*System.out.println("COMPARE >>> " + compare
+ " --------------- AND --------- Line " + line);*/
assertEquals("Comparing input and output", line, compare);
}
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("Output file does not exist");
}
}
public void assertEquals(String msg, Object expected, Object actual) {
collector.checkThat(actual, CoreMatchers.equalTo(expected));
}
}
此代码有助于触发测试方法并打印适当的方法。您能告诉我如何使用我的通用方法吗?它使用两个参数来使用ErrorCollector
如何为带有参数的junit测试方法使用错误收集器注释
@Test
不支持带有签名中参数的测试方法
示例:
尝试运行方法brokenTest
将引发异常。JUnit中正确的测试方法应该类似于correctTest
:
/** This method uses parameters in the signature. It will not work! */
@Test
public void brokenTest(String fileName) {...}
/** This correct test method has no parameters in its signature. */
@Test
public void correctTest() {...}
使用JUnit的参数化测试
要支持参数化测试,可以使用@RunWith(parameterized.class)注释(类级别)
测试类需要一个静态方法,该方法返回Iterable
中的参数(如列表
对象)。用@参数
注释此方法
此外,对于所使用的每个参数,您需要一个公共
(!)成员变量,每个变量都用@参数(0)
,@参数(1)
,等等进行注释
因此,JUnit将为使用createParameters()
方法生成的每个测试用例运行testWithParameters()。它将自动为@Parameter(N)字段(firstParameter/secondParameter)分配正确的参数
您可以根据需要生成任意多个参数
根据需要,通过引用这些参数的字段名,在测试方法中使用这些参数
您提供的课程摘录示例:
@RunWith(Parameterized.class)
public class DGNTester {
@Rule
public ErrorCollector collector = new ErrorCollector();
/**
* Method that generates the parameters.
* Each testValues.add(...) line produces a new test case.
*
* @return Array with test values.
*/
@Parameters
public static Iterable<Object[]> createParameters() {
List<Object[]> testValues = new ArrayList<>();
try {
testValues.add(new Object[]{"pre-Case1-Value1", "Case1-Value2"});
testValues.add(new Object[]{"Case2-Param1", "Case2-Value2"});
testValues.add(new Object[]{"pre-Case3-Value1", "Case3-Value2"});
}
return testValues;
}
/** The first parameter. */
@Parameter(0)
public String firstParameter;
/** The second parameter. */
@Parameter(1)
public String secondParameter;
/** Test using the parameters generated by createParameters().
* In this example we check, if the first parameter is equal to the
* concatenation of the String "pre-" and the second parameter */
@Test
public void testWithParameters() {
assertThat("Wrong parameter values", firstParameter,
is("pre-" + secondParameter));
}
...
}
@RunWith(参数化的.class)
公共级DGNTester{
@统治
public ErrorCollector=新ErrorCollector();
/**
*方法生成参数。
*每个testValues.add(…)行生成一个新的测试用例。
*
*@返回带有测试值的数组。
*/
@参数
公共静态Iterable createParameters(){
List testValues=new ArrayList();
试一试{
添加(新对象[]{“pre-Case1-Value1”,“Case1-Value2”});
添加(新对象[]{“Case2-Param1”,“Case2-Value2”});
添加(新对象[]{“pre-Case3-Value1”,“Case3-Value2”});
}
返回测试值;
}
/**第一个参数*/
@参数(0)
公共字符串参数;
/**第二个参数*/
@参数(1)
公共字符串参数;
/**使用createParameters()生成的参数进行测试。
*在本例中,我们检查第一个参数是否等于
*字符串“pre-”和第二个参数的串联*/
@试验
公共void testWithParameters(){
断言(“错误的参数值”,firstParameter,
是(“前-”+第二参数);
}
...
}
@RunWith(Parameterized.class)
public class DGNTester {
@Rule
public ErrorCollector collector = new ErrorCollector();
/**
* Method that generates the parameters.
* Each testValues.add(...) line produces a new test case.
*
* @return Array with test values.
*/
@Parameters
public static Iterable<Object[]> createParameters() {
List<Object[]> testValues = new ArrayList<>();
try {
testValues.add(new Object[]{"pre-Case1-Value1", "Case1-Value2"});
testValues.add(new Object[]{"Case2-Param1", "Case2-Value2"});
testValues.add(new Object[]{"pre-Case3-Value1", "Case3-Value2"});
}
return testValues;
}
/** The first parameter. */
@Parameter(0)
public String firstParameter;
/** The second parameter. */
@Parameter(1)
public String secondParameter;
/** Test using the parameters generated by createParameters().
* In this example we check, if the first parameter is equal to the
* concatenation of the String "pre-" and the second parameter */
@Test
public void testWithParameters() {
assertThat("Wrong parameter values", firstParameter,
is("pre-" + secondParameter));
}
...
}