Why won't this Expect Exception Junit Test Work?

前端 未结 2 2010
灰色年华
灰色年华 2021-01-27 14:06

This is my test so far:

@Test(expected = FileNotFoundException.class)
public void testExpectedException() {
    UserDataObject u = new UserDataObject(\"rob123\",         


        
相关标签:
2条回答
  • 2021-01-27 14:33

    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

    0 讨论(0)
  • 2021-01-27 14:35

    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.

    0 讨论(0)
提交回复
热议问题