2016-12-27

AssertJ - assertThatExceptionOfType

Przykład alternatywnego testowania wyjątku - assertThatExceptionOfType
public class FooTest {

    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    private Validator validator;

    @InjectMocks
    private Foo sut;

    private HistoryRequest historyRequest;

    @Before
    public void setUp() throws Exception {
        historyRequest = new HistoryRequest();
    }

    @Test
    public void getHistoryThrowsExceptionDueToAccessDenied() throws Exception {
        when(validator.validate(historyRequest)).thenReturn(new Client());

        assertThatExceptionOfType(BloggerException.class)
                .isThrownBy(() -> sut.getHistory(historyRequest))
                .withMessage("Access denied");
    }
}

Brak komentarzy:

Prześlij komentarz