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

时间:2015-02-15 20:23:37

标签: java exception junit assertions

我在使JUnit测试工作时遇到了一些问题。我想测试方法,如果它找到文件或如果它没有,如果它捕获它,但我没有得到预期的结果。我的文件名是studenter,结果和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);        
}

这是我的班级:

    @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)

1 个答案:

答案 0 :(得分:1)

您的测试永远不会通过,因为您正在捕捉IOException而不是进一步重新抛出它:

catch (IOException ex) {
    Logger.getLogger(Grader.class.getName()).log(Level.SEVERE, null, ex);
    throw ex; // add this line
}

您应该重新抛出异常

你可以删除FileNotFoundException,因为它扩展了IOException,所以两者都可以按我建议的方式处理。