Why won't this Expect Exception Junit Test Work?

ぃ、小莉子 提交于 2019-12-02 20:31:45

问题


This is my test so far:

@Test(expected = FileNotFoundException.class)
public void testExpectedException() {
    UserDataObject u = new UserDataObject("rob123", "Smith", "Robert", "Danny", 1);
    FlatFileStorage ffstorage = new FlatFileStorage();
    ffstorage.verifyUser(u, "dsfsdfsfdsdf.txt");

However, the test seems to fail! Even though when I run it, the error message actually gets printed out from the method on the console too, like this

dsfsdfsfdsdf.txt (The system cannot find the file specified)

Am I missing something really obvious here?

EDIT** Here is the method I'm actually testing

public void verifyUser(UserDataObject u, String filename) {

    String eachline = "";
    String uname = u.getUsername();
    Scanner sc;


    try {
        BufferedReader br = new BufferedReader(new FileReader(new File(filename)));

            StringBuilder sb = new StringBuilder();
            while ((eachline = br.readLine())!=null) 
            {
                if(eachline.indexOf("Username: " + uname)!=-1)
                {
                    int lineStart = eachline.indexOf("Username: " + uname);
                    int lineEnd = eachline.indexOf("end" + uname + ";");
                    int unameLength = uname.length();

                    String lineStatus = eachline.substring(lineStart, lineEnd + 4 + unameLength);
                    //System.out.println(lineStatus);

                    String newLineStatus = lineStatus.replace("VerifyStatus: 0", "VerifyStatus: 1");

                    sb.append(newLineStatus+"\n");

                }else{
                    sb.append(eachline+"\n");
                }
            } 
            br.close();
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filename)));
            PrintWriter out = new PrintWriter(bw);
            out.print(sb.toString());
            out.flush();
            out.close();
            } catch (FileNotFoundException e) {
                System.out.println("Error: Could not find database/storage.");
                System.out.println(e.getMessage()); 
                e.printStackTrace();
                } catch (IOException e) {
                    System.out.println("There has been an error: " + e.getMessage());
                    e.printStackTrace();
            }   

 }

回答1:


Your test is failing because you are expecting the FileNotFoundException, but it not actualy thrown from a method. Why? Because of try/catch wrapper, that swallows the exception, only printing the stack trace to the output.

Change the catch block to

catch (FileNotFoundException e) {
            System.out.println("Error: Could not find database/storage.");
            System.out.println(e.getMessage()); 
            throw e;
            }

and test will pass




回答2:


The issue is that your verifyUser(UserDataObject u, String filename) method catches both exceptions FileNotFoundException and IOException and hence it is not propagated to your test method.



来源:https://stackoverflow.com/questions/18043021/why-wont-this-expect-exception-junit-test-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!