Why won't this Expect Exception Junit Test Work?

前端 未结 2 2016
灰色年华
灰色年华 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

提交回复
热议问题