Unit testing Junit@BeforeClass don';我不明白
这是我的测试课。我尝试解析CSV文件并创建一个对象[](调用Unit testing Junit@BeforeClass don';我不明白,unit-testing,junit4,Unit Testing,Junit4,这是我的测试课。我尝试解析CSV文件并创建一个对象[](调用数据)。当我尝试像参数一样给出数据时,它调用空指针。我不知道为什么失败了。当我提供像硬核代码这样的数据时,它确实有效。请解释一下,我不明白 @RunWith(Parameterized.class) public class TestJUnit { public int firstParameter; public int secondParameter; public String operation; public int exp
数据
)。当我尝试像参数一样给出数据时,它调用空指针。我不知道为什么失败了。当我提供像硬核代码这样的数据时,它确实有效。请解释一下,我不明白
@RunWith(Parameterized.class)
public class TestJUnit {
public int firstParameter;
public int secondParameter;
public String operation;
public int expectedResult;
public static Object[][] data;
public TestJUnit(int firstParameter, int secondParameter, String operation, int expectedResult) {
this.firstParameter = firstParameter;
this.secondParameter = secondParameter;
this.expectedResult = expectedResult;
this.operation = operation;
}
@BeforeClass
public void makeData() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\third\\IdeaProjects\\MyJunit\\src\\test\\java\\com\\myLogicTest\\datafile.csv"));
ArrayList<Object[]> tempArray = new ArrayList<Object[]>();
String newLine;
Object[] oneString;
while ((newLine = reader.readLine()) != null) {
oneString = newLine.split(";");
tempArray.add(oneString);
}
data = new Object[tempArray.size()][];
for (int i = 0; i < data.length; i++) {
Object[] row = tempArray.get(i); data[i] = row; }
}
@RunWith(参数化的.class)
公共类TestJUnit{
公共int参数;
公共int参数;
公共字符串操作;
公众期望结果;
公共静态对象[][]数据;
公共TestJUnit(int firstParameter、int secondParameter、字符串操作、int expectedResult){
this.firstParameter=firstParameter;
this.secondParameter=secondParameter;
this.expectedResult=expectedResult;
这个操作=操作;
}
@课前
public void makeData()引发IOException{
BufferedReader=new BufferedReader(新文件阅读器(“C:\\Users\\third\\IdeaProjects\\MyJunit\\src\\test\\java\\com\\MyLogictTest\\datafile.csv”);
ArrayList tempArray=新的ArrayList();
字符串换行符;
对象[]一个字符串;
而((newLine=reader.readLine())!=null){
oneString=newLine.split(“;”);
添加(oneString);
}
数据=新对象[tempArray.size()][];
对于(int i=0;i
@测试
公共无效校验计算器(){
最终计算器=新计算器(第一个参数、第二个参数、操作);
int结果;
if(操作。等于(“*”){
结果=calculator.multi();
Assert.assertTrue(“结果+”)或“预期结果,结果==预期结果”);
}
else if(运算.等于(“+”)){
结果=计算器.plus();
Assert.assertTrue(“结果+”)或“预期结果,结果==预期结果”);
}
else if(运算.等于(“-”){
结果=计算器。减号();
Assert.assertTrue(“结果+”)或“预期结果,结果==预期结果”);
}
else if(操作.等于(“/”){
结果=calculator.del();
Assert.assertTrue(“结果+”)或“预期结果,结果==预期结果”);
}
}
@Parameterized.Parameters(name=“{index}:СчСччччч{0}{2}{1}={3}”)
公共静态集合getTestData(){
返回Arrays.asList(数据
/*新对象[][]{/问题出现在注释@BeforeClass中,当我们使用参数化测试时,@Before和@BeforeClass运行并不是第一个!在我的情况下,我需要在方法getTestData()中调用方法makeData(),如下所示:
@Parameterized.Parameters(name = "{index}: Действие {0} {2} {1} = {3}")
public static Collection<Object[]> getTestData() {
makedata(); // <<<<<I NEED USE IT HERE!
return Arrays.asList(data
/* new Object[][]{ //<<<NULLPOINTER HERE
{2, 2, "*", 4},
{2, 0, "+" , 2},
{2, 2,"/", 1},
{0, 2,"-",-2}
}*/);
@Parameterized.Parameters(name=“{index}:БСчччч{0}{2}{1}={3}”)
公共静态集合getTestData(){
MakedData();//此处的空指针行似乎用/*字符进行了注释。这甚至会给您一个编译错误,甚至在运行时错误之前。@RobertoLinares,不,此注释用于在硬代码模式下测试此代码,我只给他一个新对象[],没有“数据”,没有更多
@Parameterized.Parameters(name = "{index}: Действие {0} {2} {1} = {3}")
public static Collection<Object[]> getTestData() {
makedata(); // <<<<<I NEED USE IT HERE!
return Arrays.asList(data
/* new Object[][]{ //<<<NULLPOINTER HERE
{2, 2, "*", 4},
{2, 0, "+" , 2},
{2, 2,"/", 1},
{0, 2,"-",-2}
}*/);