Junit 测试预期异常:java.io.FileNotFoundException、junit.framework.assertionFailedError
Junit testing Expected exception: java.io.FileNotFoundException, junit.framework.assertionFailedError
我在使 JUnit 测试正常工作时遇到了一些问题。我想测试该方法是否找到文件,或者是否没有找到文件以及是否捕获了文件,但我没有得到预期的结果。我的文件名是 studenter、results 和 uppgift。这是我格式化的测试。
@Test(expected = java.io.FileNotFoundException.class)
public void testGradeAndPrintFileEX() throws Exception {
System.out.println("gradeAndPrintFile");
String assignmentFile = "uppgsift";
String studentFile = "studentder";
String resultFile = "resuslts";
Grader instance = new Grader();
instance.gradeAndPrintFile(assignmentFile, studentFile, resultFile);
}
@Test(expected = java.io.IOException.class)
public void testGradeAndPrintFileEX2() throws Exception {
System.out.println("gradeAndPrintFile");
String assignmentFile = "uppgift";
String studentFile = "studenter";
String resultFile = "results";
Grader instance = new Grader();
instance.gradeAndPrintFile(assignmentFile, studentFile, resultFile);
}
这是我的 class:
@Override
public void gradeAndPrintFile(String assignmentFile, String studentFile, String resultFile) throws FileNotFoundException, IOException {
student = new Student();
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
try {
FileWriter file = new FileWriter(resultFile);
BufferedReader studentReader = new BufferedReader(new FileReader(studentFile));
BufferedReader assignmentReader = new BufferedReader(new FileReader(new File(assignmentFile)));
String assagning;
Map<String, Integer> hashMap = new HashMap<>();
while ((assagning = assignmentReader.readLine()) != null) {
String[] cut = assagning.split(";");
hashMap.put(cut[0], Integer.parseInt(cut[1]));
}
assignment = new Assignment();
assignment.setLevelForPass(hashMap.get("G"));
assignment.setLevelForPassWithDistinction(hashMap.get("VG"));
String studenter;
String[] studentArray;
while ((studenter = studentReader.readLine()) != null) {
studentArray = studenter.split(";");
eMail = studentArray[2];
lastName = studentArray[0];
firstName = studentArray[1];
student.setEmail(eMail);
student.setLastName(lastName);
student.setFirstName(firstName);
ArrayList<Integer> poangLista = new ArrayList<>();
for (int i = 0; i < studentArray.length; i++) {
if (i > 2) {
String numbers = studentArray[i];
poangLista.add(Integer.parseInt(numbers));
}
}
grade = new Grade();
grade.setUp(student, poangLista, assignment);
System.out.println(grade.toString());
file.write(grade.toString() + "\n");
file.flush();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Grader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Grader.class.getName()).log(Level.SEVERE, null, ex);
}
}
我得到的错误是一堆红色行我不知道它们是什么意思,它们如下:
feb 15, 2015 9:05:19 EM grader.Grader gradeAndPrintFile
ALLVARLIG: null
java.io.FileNotFoundException: studentder (Det går inte att hitta filen)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:131)
at java.io.FileInputStream.<init>(FileInputStream.java:87)
at java.io.FileReader.<init>(FileReader.java:58)
at grader.Grader.gradeAndPrintFile(Grader.java:47)
at grader.GraderTest.testGradeAndPrintFileEX(GraderTest.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:22)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:532)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1179)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:1030)
您的测试永远不会通过,因为您正在捕获 IOException
而不是进一步重新抛出它:
catch (IOException ex) {
Logger.getLogger(Grader.class.getName()).log(Level.SEVERE, null, ex);
throw ex; // add this line
}
您应该重新抛出异常。
您可以删除 FileNotFoundException
的捕获,因为它扩展了 IOException
,因此两者都可以按照我的建议由单个块处理。
我在使 JUnit 测试正常工作时遇到了一些问题。我想测试该方法是否找到文件,或者是否没有找到文件以及是否捕获了文件,但我没有得到预期的结果。我的文件名是 studenter、results 和 uppgift。这是我格式化的测试。
@Test(expected = java.io.FileNotFoundException.class)
public void testGradeAndPrintFileEX() throws Exception {
System.out.println("gradeAndPrintFile");
String assignmentFile = "uppgsift";
String studentFile = "studentder";
String resultFile = "resuslts";
Grader instance = new Grader();
instance.gradeAndPrintFile(assignmentFile, studentFile, resultFile);
}
@Test(expected = java.io.IOException.class)
public void testGradeAndPrintFileEX2() throws Exception {
System.out.println("gradeAndPrintFile");
String assignmentFile = "uppgift";
String studentFile = "studenter";
String resultFile = "results";
Grader instance = new Grader();
instance.gradeAndPrintFile(assignmentFile, studentFile, resultFile);
}
这是我的 class:
@Override
public void gradeAndPrintFile(String assignmentFile, String studentFile, String resultFile) throws FileNotFoundException, IOException {
student = new Student();
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
try {
FileWriter file = new FileWriter(resultFile);
BufferedReader studentReader = new BufferedReader(new FileReader(studentFile));
BufferedReader assignmentReader = new BufferedReader(new FileReader(new File(assignmentFile)));
String assagning;
Map<String, Integer> hashMap = new HashMap<>();
while ((assagning = assignmentReader.readLine()) != null) {
String[] cut = assagning.split(";");
hashMap.put(cut[0], Integer.parseInt(cut[1]));
}
assignment = new Assignment();
assignment.setLevelForPass(hashMap.get("G"));
assignment.setLevelForPassWithDistinction(hashMap.get("VG"));
String studenter;
String[] studentArray;
while ((studenter = studentReader.readLine()) != null) {
studentArray = studenter.split(";");
eMail = studentArray[2];
lastName = studentArray[0];
firstName = studentArray[1];
student.setEmail(eMail);
student.setLastName(lastName);
student.setFirstName(firstName);
ArrayList<Integer> poangLista = new ArrayList<>();
for (int i = 0; i < studentArray.length; i++) {
if (i > 2) {
String numbers = studentArray[i];
poangLista.add(Integer.parseInt(numbers));
}
}
grade = new Grade();
grade.setUp(student, poangLista, assignment);
System.out.println(grade.toString());
file.write(grade.toString() + "\n");
file.flush();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Grader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Grader.class.getName()).log(Level.SEVERE, null, ex);
}
}
我得到的错误是一堆红色行我不知道它们是什么意思,它们如下:
feb 15, 2015 9:05:19 EM grader.Grader gradeAndPrintFile
ALLVARLIG: null
java.io.FileNotFoundException: studentder (Det går inte att hitta filen)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:131)
at java.io.FileInputStream.<init>(FileInputStream.java:87)
at java.io.FileReader.<init>(FileReader.java:58)
at grader.Grader.gradeAndPrintFile(Grader.java:47)
at grader.GraderTest.testGradeAndPrintFileEX(GraderTest.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:22)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:532)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1179)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:1030)
您的测试永远不会通过,因为您正在捕获 IOException
而不是进一步重新抛出它:
catch (IOException ex) {
Logger.getLogger(Grader.class.getName()).log(Level.SEVERE, null, ex);
throw ex; // add this line
}
您应该重新抛出异常。
您可以删除 FileNotFoundException
的捕获,因为它扩展了 IOException
,因此两者都可以按照我的建议由单个块处理。