Skip to content

Commit 5ff9e69

Browse files
committed
Test status quo for void function references in SpEL
1 parent 0cb4043 commit 5ff9e69

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.StringTokenizer;
3030

3131
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.params.ParameterizedTest;
33+
import org.junit.jupiter.params.provider.ValueSource;
3234

3335
import org.springframework.asm.MethodVisitor;
3436
import org.springframework.expression.AccessException;
@@ -55,6 +57,7 @@
5557
* Checks SpelCompiler behavior. This should cover compilation all compiled node types.
5658
*
5759
* @author Andy Clement
60+
* @author Sam Brannen
5861
* @since 4.1
5962
*/
6063
public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -997,6 +1000,60 @@ public void functionReference() throws Exception {
9971000
assertThat(expression.getValue(ctx).toString()).isEqualTo("4.0");
9981001
}
9991002

1003+
@ParameterizedTest
1004+
@ValueSource(strings = {"voidMethod", "voidWrapperMethod"})
1005+
public void voidFunctionReference(String method) throws Exception {
1006+
assertVoidFunctionReferenceBehavior(method);
1007+
}
1008+
1009+
private void assertVoidFunctionReferenceBehavior(String methodName) throws Exception {
1010+
Method method = getClass().getDeclaredMethod(methodName, String.class);
1011+
1012+
EvaluationContext ctx = new StandardEvaluationContext();
1013+
ctx.setVariable("voidMethod", method);
1014+
1015+
expression = parser.parseExpression("#voidMethod('a')");
1016+
1017+
voidMethodInvokedWith = null;
1018+
expression.getValue(ctx);
1019+
assertThat(voidMethodInvokedWith).isEqualTo("a");
1020+
assertCanCompile(expression);
1021+
1022+
voidMethodInvokedWith = null;
1023+
expression.getValue(ctx);
1024+
assertThat(voidMethodInvokedWith).isEqualTo("a");
1025+
assertCanCompile(expression);
1026+
1027+
voidMethodInvokedWith = null;
1028+
expression.getValue(ctx);
1029+
assertThat(voidMethodInvokedWith).isEqualTo("a");
1030+
assertCanCompile(expression);
1031+
1032+
expression = parser.parseExpression("#voidMethod(#a)");
1033+
ctx.setVariable("a", "foo");
1034+
1035+
voidMethodInvokedWith = null;
1036+
expression.getValue(ctx);
1037+
assertThat(voidMethodInvokedWith).isEqualTo("foo");
1038+
assertCanCompile(expression);
1039+
1040+
voidMethodInvokedWith = null;
1041+
expression.getValue(ctx);
1042+
assertThat(voidMethodInvokedWith).isEqualTo("foo");
1043+
assertCanCompile(expression);
1044+
}
1045+
1046+
private static String voidMethodInvokedWith;
1047+
1048+
public static Void voidWrapperMethod(String str) {
1049+
voidMethodInvokedWith = str;
1050+
return null;
1051+
}
1052+
1053+
public static void voidMethod(String str) {
1054+
voidMethodInvokedWith = str;
1055+
}
1056+
10001057
@Test
10011058
public void functionReferenceVisibility_SPR12359() throws Exception {
10021059
// Confirms visibility of what is being called.

0 commit comments

Comments
 (0)