Post

Java getMessage vs toString For NullPointerException

1
2
3
4
5
6
7
8
9
10
11
12
class TestNullPointerException {
    @Test
    void testNullPointerException() {
        Throwable expectedException = assertThrows(NullPointerException.class, () -> {
            throw new NullPointerException();
        });
        assertAll("Expect NullPointerException.getMessage() is null; .toString() is the class name",
            () -> assertNull(expectedException.getMessage()),
            () -> assertEquals("java.lang.NullPointerException", expectedException.toString())
        );
    }
}
This post is licensed under CC BY 4.0 by the author.