gpt4 book ai didi

java - Junit测试预期异常: java. io.FileNotFoundException,junit.framework.assertionFailedError

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:30 26 4
gpt4 key购买 nike

我在使 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$1.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$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.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,因此两者都可以按照我的建议由单个 block 处理。

关于java - Junit测试预期异常: java. io.FileNotFoundException,junit.framework.assertionFailedError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28530731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com