Skip to content

Commit e60b291

Browse files
committed
add utility for nullchecking
1 parent 30a2fb9 commit e60b291

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package it.aboutbits.springboot.toolbox.util;
2+
3+
import org.jspecify.annotations.NullMarked;
4+
import org.jspecify.annotations.Nullable;
5+
6+
@NullMarked
7+
public final class NullUtil {
8+
private NullUtil() {
9+
}
10+
11+
public static <T> T nonNullOrFail(@Nullable T x) {
12+
if (x == null) {
13+
throw new IllegalStateException("Required non-null value, but got null.");
14+
}
15+
return x;
16+
}
17+
18+
public static <T> T nonNullOrFail(@Nullable T x, String failureMessage) {
19+
if (x == null) {
20+
throw new IllegalStateException(failureMessage);
21+
}
22+
return x;
23+
}
24+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package it.aboutbits.springboot.toolbox.util;
2+
3+
import org.jspecify.annotations.NullMarked;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
8+
9+
@NullMarked
10+
class NullUtilTest {
11+
12+
@Test
13+
void nonNullOrFail_givenNonNullValue_shouldReturnValue() {
14+
// given
15+
var value = "test";
16+
17+
// when
18+
var result = NullUtil.nonNullOrFail(value);
19+
20+
// then
21+
assertThat(result).isEqualTo(value);
22+
}
23+
24+
@Test
25+
void nonNullOrFail_givenNullValue_shouldThrowIllegalStateException() {
26+
// given
27+
String value = null;
28+
29+
// then
30+
assertThatIllegalStateException()
31+
.isThrownBy(() -> /* when */ NullUtil.nonNullOrFail(value))
32+
.withMessage("Required non-null value, but got null.");
33+
}
34+
35+
@Test
36+
void nonNullOrFailWithMessage_givenNonNullValue_shouldReturnValue() {
37+
// given
38+
var value = "test";
39+
var message = "Custom error message";
40+
41+
// when
42+
var result = NullUtil.nonNullOrFail(value, message);
43+
44+
// then
45+
assertThat(result).isEqualTo(value);
46+
}
47+
48+
@Test
49+
void nonNullOrFailWithMessage_givenNullValue_shouldThrowIllegalStateExceptionWithCustomMessage() {
50+
// given
51+
String value = null;
52+
var message = "Custom error message";
53+
54+
// then
55+
assertThatIllegalStateException()
56+
.isThrownBy(() -> /* when */ NullUtil.nonNullOrFail(value, message))
57+
.withMessage(message);
58+
}
59+
60+
@Test
61+
void nonNullOrFail_givenNonNullObject_shouldReturnSameInstance() {
62+
// given
63+
var value = new Object();
64+
65+
// when
66+
var result = NullUtil.nonNullOrFail(value);
67+
68+
// then
69+
assertThat(result).isSameAs(value);
70+
}
71+
72+
@Test
73+
void nonNullOrFailWithMessage_givenNonNullObject_shouldReturnSameInstance() {
74+
// given
75+
var value = new Object();
76+
var message = "Custom error message";
77+
78+
// when
79+
var result = NullUtil.nonNullOrFail(value, message);
80+
81+
// then
82+
assertThat(result).isSameAs(value);
83+
}
84+
}

0 commit comments

Comments
 (0)