assertthat code throws exception

Solutions on MaxInterview for assertthat code throws exception by the best coders in the world

showing results for - "assertthat code throws exception"
Nicole
26 Aug 2020
1@Test
2public void test_exception_approach_1() {
3    ...
4    assertThatExceptionOfType(IOException.class)
5            .isThrownBy(() -> someBadIOOperation())
6            .withMessage("boom!"); 
7}
8
9@Test
10public void test_exception_approach_2() {
11    ...
12    assertThatThrownBy(() -> someBadIOOperation())
13            .isInstanceOf(Exception.class)
14            .hasMessageContaining("boom");
15}
16
17@Test
18public void test_exception_approach_3() {
19    ...
20    // when
21    Throwable thrown = catchThrowable(() -> someBadIOOperation());
22
23    // then
24    assertThat(thrown).isInstanceOf(Exception.class)
25                      .hasMessageContaining("boom");
26}
27