Optimize test code with lambdas where feasible
This commit optimizes test code with lambda expressions and method references where feasible. Closes gh-23626
This commit is contained in:
parent
00c07e3a50
commit
40fcf876ce
|
|
@ -277,14 +277,11 @@ public class MethodInvocationTests extends AbstractExpressionTests {
|
|||
final BytesService service = new BytesService();
|
||||
byte[] bytes = new byte[100];
|
||||
StandardEvaluationContext context = new StandardEvaluationContext(bytes);
|
||||
context.setBeanResolver(new BeanResolver() {
|
||||
@Override
|
||||
public Object resolve(EvaluationContext context, String beanName) throws AccessException {
|
||||
if ("service".equals(beanName)) {
|
||||
return service;
|
||||
}
|
||||
return null;
|
||||
context.setBeanResolver((context1, beanName) -> {
|
||||
if ("service".equals(beanName)) {
|
||||
return service;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
Expression expression = parser.parseExpression("@service.handleBytes(#root)");
|
||||
byte[] outBytes = expression.getValue(context, byte[].class);
|
||||
|
|
|
|||
|
|
@ -5156,8 +5156,7 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
|
|||
}
|
||||
|
||||
private void assertGetValueFail(Expression expression) {
|
||||
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
|
||||
expression.getValue());
|
||||
assertThatExceptionOfType(Exception.class).isThrownBy(expression::getValue);
|
||||
}
|
||||
|
||||
private void assertIsCompiled(Expression expression) {
|
||||
|
|
|
|||
|
|
@ -1326,18 +1326,14 @@ public class SpelReproTests extends AbstractExpressionTests {
|
|||
@Override
|
||||
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name,
|
||||
List<TypeDescriptor> argumentTypes) throws AccessException {
|
||||
return new MethodExecutor() {
|
||||
@Override
|
||||
public TypedValue execute(EvaluationContext context, Object target, Object... arguments)
|
||||
throws AccessException {
|
||||
try {
|
||||
Method method = XYZ.class.getMethod("values");
|
||||
Object value = method.invoke(target, arguments);
|
||||
return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new AccessException(ex.getMessage(), ex);
|
||||
}
|
||||
return (context1, target, arguments) -> {
|
||||
try {
|
||||
Method method = XYZ.class.getMethod("values");
|
||||
Object value = method.invoke(target, arguments);
|
||||
return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new AccessException(ex.getMessage(), ex);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -345,8 +345,7 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
|
|||
assertThat(property.read(ctx, tester, "property").getValue()).isEqualTo("hello");
|
||||
// cached accessor used
|
||||
assertThat(property.read(ctx, tester, "property").getValue()).isEqualTo("hello");
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
property.getSpecificTargetClasses());
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(property::getSpecificTargetClasses);
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
property.write(ctx, tester, "property", null));
|
||||
|
||||
|
|
@ -360,8 +359,7 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
|
|||
assertThat(field.read(ctx, tester, "field").getValue()).isEqualTo(3);
|
||||
// cached accessor used
|
||||
assertThat(field.read(ctx, tester, "field").getValue()).isEqualTo(3);
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
field.getSpecificTargetClasses());
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(field::getSpecificTargetClasses);
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
field.write(ctx, tester, "field", null));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue