Skip to content

Commit 1fe2216

Browse files
committed
Test status quo for null-safe Void method references in SpEL
1 parent 5ff9e69 commit 1fe2216

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3898,6 +3898,40 @@ void methodReferenceVarargs() {
38983898
tc.reset();
38993899
}
39003900

3901+
@Test
3902+
void nullSafeInvocationOfNonStaticVoidWrapperMethod() {
3903+
// non-static method, no args, Void return
3904+
expression = parser.parseExpression("new %s()?.oneVoidWrapper()".formatted(TestClass5.class.getName()));
3905+
3906+
assertCantCompile(expression);
3907+
3908+
TestClass5._i = 0;
3909+
expression.getValue();
3910+
assertThat(TestClass5._i).isEqualTo(1);
3911+
3912+
TestClass5._i = 0;
3913+
assertCanCompile(expression);
3914+
expression.getValue();
3915+
assertThat(TestClass5._i).isEqualTo(1);
3916+
}
3917+
3918+
@Test
3919+
void nullSafeInvocationOfStaticVoidWrapperMethod() {
3920+
// static method, no args, Void return
3921+
expression = parser.parseExpression("T(%s)?.twoVoidWrapper()".formatted(TestClass5.class.getName()));
3922+
3923+
assertCantCompile(expression);
3924+
3925+
TestClass5._i = 0;
3926+
expression.getValue();
3927+
assertThat(TestClass5._i).isEqualTo(1);
3928+
3929+
TestClass5._i = 0;
3930+
assertCanCompile(expression);
3931+
expression.getValue();
3932+
assertThat(TestClass5._i).isEqualTo(1);
3933+
}
3934+
39013935
@Test
39023936
void methodReference() {
39033937
TestClass5 tc = new TestClass5();
@@ -5712,8 +5746,19 @@ public void reset() {
57125746

57135747
public void one() { i = 1; }
57145748

5749+
public Void oneVoidWrapper() {
5750+
_i = 1;
5751+
this.i = 1;
5752+
return null;
5753+
}
5754+
57155755
public static void two() { _i = 1; }
57165756

5757+
public static Void twoVoidWrapper() {
5758+
_i = 1;
5759+
return null;
5760+
}
5761+
57175762
public String three() { return "hello"; }
57185763
public long four() { return 3277700L; }
57195764

0 commit comments

Comments
 (0)