-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExceptionTest.java
39 lines (29 loc) · 1.18 KB
/
ExceptionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.github.hubertwo.playground.java15.exception;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class ExceptionTest {
@Test
@DisplayName("Code details in exception messages")
public void descriptiveNullPointerException() {
Kettle kettle = new Kettle(/*isPluggedIn*/ true, /*isFilled*/ null);
NullPointerException actualException = assertThrows(NullPointerException.class, kettle::boilWater);
assertThat(actualException).hasMessage("""
Cannot invoke "java.lang.Boolean.booleanValue()" because "this.isFilled" is null""");
}
private static class Kettle {
private final Boolean isPluggedIn;
private final Boolean isFilled;
public Kettle(Boolean isPluggedIn, Boolean isFilled) {
this.isPluggedIn = isPluggedIn;
this.isFilled = isFilled;
}
public void boilWater() {
if (Boolean.logicalAnd(isFilled, isPluggedIn)) {
// boil the water
}
// Some logic
}
}
}