Mockito verify after exception Junit 4.10

前端 未结 4 1147
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 22:19

I am testing a method with an expected exception. I also need to verify that some cleanup code was called (on a mocked object) after the exception is thrown, but it looks li

4条回答
  •  [愿得一人]
    2020-12-05 22:51

    More elegant solution with catch-exception

    @Test
    public void testExpectedException()
    {
        MockedObject mockObj = mock(MockedObject.class);
        MySubject subject = new MySubject(mockObj);
    
        when(subject).someMethodThrowingException();
    
        then(caughtException())
                .isInstanceOf(MyException.class)
                .hasMessage("My exception message.");
    
        verify(mockObj).someCleanup(eq(...));
    }
    

提交回复
热议问题