Skip to content

Commit 1fe40cf

Browse files
committed
Fix failing tests according new changes in SF SpEL module
Related to: spring-projects/spring-framework#33174
1 parent c9f4d7e commit 1fe40cf

File tree

2 files changed

+41
-71
lines changed

2 files changed

+41
-71
lines changed

spring-integration-core/src/test/java/org/springframework/integration/handler/MethodInvokingMessageProcessorTests.java

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ public void optionalAndRequiredWithAnnotatedMethod() throws Exception {
500500
Method method = service.getClass().getMethod("optionalAndRequiredHeader", String.class, Integer.class);
501501
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, method);
502502
processor.setUseSpelInvoker(true);
503-
optionalAndRequiredWithAnnotatedMethodGuts(processor, false);
503+
optionalAndRequiredWithAnnotatedMethodGuts(processor);
504504
}
505505

506506
@Test
@@ -510,14 +510,13 @@ public void compiledOptionalAndRequiredWithAnnotatedMethod() throws Exception {
510510
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, method);
511511
processor.setUseSpelInvoker(true);
512512
DirectFieldAccessor compilerConfigAccessor = compileImmediate(processor);
513-
optionalAndRequiredWithAnnotatedMethodGuts(processor, true);
513+
optionalAndRequiredWithAnnotatedMethodGuts(processor);
514514
assertThat(TestUtils.getPropertyValue(processor, "delegate.handlerMethod.expression.compiledAst")).isNotNull();
515-
optionalAndRequiredWithAnnotatedMethodGuts(processor, true);
515+
optionalAndRequiredWithAnnotatedMethodGuts(processor);
516516
compilerConfigAccessor.setPropertyValue("compilerMode", SpelCompilerMode.OFF);
517517
}
518518

519-
private void optionalAndRequiredWithAnnotatedMethodGuts(MethodInvokingMessageProcessor processor,
520-
boolean compiled) {
519+
private void optionalAndRequiredWithAnnotatedMethodGuts(MethodInvokingMessageProcessor processor) {
521520

522521
processor.setBeanFactory(mock(BeanFactory.class));
523522
Message<String> message = MessageBuilder.withPayload("foo")
@@ -531,21 +530,14 @@ private void optionalAndRequiredWithAnnotatedMethodGuts(MethodInvokingMessagePro
531530
.build();
532531
result = processor.processMessage(message);
533532
assertThat(result).isEqualTo("bar42");
534-
message = MessageBuilder.withPayload("foo")
535-
.setHeader("prop", "bar")
536-
.build();
537-
try {
538-
result = processor.processMessage(message);
539-
fail("Expected MessageHandlingException");
540-
}
541-
catch (MessageHandlingException e) {
542-
if (compiled) {
543-
assertThat(e.getCause().getMessage()).isEqualTo("required header not available: num");
544-
}
545-
else {
546-
assertThat(e.getCause().getCause().getMessage()).isEqualTo("required header not available: num");
547-
}
548-
}
533+
534+
assertThatExceptionOfType(MessageHandlingException.class)
535+
.isThrownBy(() ->
536+
processor.processMessage(
537+
MessageBuilder.withPayload("foo")
538+
.setHeader("prop", "bar")
539+
.build()))
540+
.withStackTraceContaining("required header not available: num");
549541
}
550542

551543
@Test
@@ -555,7 +547,7 @@ public void optionalAndRequiredDottedWithAnnotatedMethod() throws Exception {
555547
String.class);
556548
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, method);
557549
processor.setUseSpelInvoker(true);
558-
optionalAndRequiredDottedWithAnnotatedMethodGuts(processor, false);
550+
optionalAndRequiredDottedWithAnnotatedMethodGuts(processor);
559551
}
560552

561553
@Test
@@ -566,14 +558,13 @@ public void compiledOptionalAndRequiredDottedWithAnnotatedMethod() throws Except
566558
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, method);
567559
processor.setUseSpelInvoker(true);
568560
DirectFieldAccessor compilerConfigAccessor = compileImmediate(processor);
569-
optionalAndRequiredDottedWithAnnotatedMethodGuts(processor, true);
561+
optionalAndRequiredDottedWithAnnotatedMethodGuts(processor);
570562
assertThat(TestUtils.getPropertyValue(processor, "delegate.handlerMethod.expression.compiledAst")).isNotNull();
571-
optionalAndRequiredDottedWithAnnotatedMethodGuts(processor, true);
563+
optionalAndRequiredDottedWithAnnotatedMethodGuts(processor);
572564
compilerConfigAccessor.setPropertyValue("compilerMode", SpelCompilerMode.OFF);
573565
}
574566

575-
private void optionalAndRequiredDottedWithAnnotatedMethodGuts(MethodInvokingMessageProcessor processor,
576-
boolean compiled) {
567+
private void optionalAndRequiredDottedWithAnnotatedMethodGuts(MethodInvokingMessageProcessor processor) {
577568

578569
processor.setBeanFactory(mock(BeanFactory.class));
579570
Message<String> message = MessageBuilder.withPayload("hello")
@@ -589,22 +580,15 @@ private void optionalAndRequiredDottedWithAnnotatedMethodGuts(MethodInvokingMess
589580
.build();
590581
result = processor.processMessage(message);
591582
assertThat(result).isEqualTo("bar42dotted");
592-
message = MessageBuilder.withPayload("hello")
593-
.setHeader("dot1", new DotBean())
594-
.setHeader("dotted.literal", "dotted")
595-
.build();
596-
try {
597-
result = processor.processMessage(message);
598-
fail("Expected MessageHandlingException");
599-
}
600-
catch (MessageHandlingException e) {
601-
if (compiled) {
602-
assertThat(e.getCause().getMessage()).isEqualTo("required header not available: dot2");
603-
}
604-
else { // interpreted
605-
assertThat(e.getCause().getCause().getMessage()).isEqualTo("required header not available: dot2");
606-
}
607-
}
583+
584+
assertThatExceptionOfType(MessageHandlingException.class)
585+
.isThrownBy(() ->
586+
processor.processMessage(
587+
MessageBuilder.withPayload("hello")
588+
.setHeader("dot1", new DotBean())
589+
.setHeader("dotted.literal", "dotted")
590+
.build()))
591+
.withStackTraceContaining("required header not available: dot2");
608592
}
609593

610594
@Test

spring-integration-core/src/test/java/org/springframework/integration/json/JsonPathTests.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
import com.jayway.jsonpath.Filter;
2626
import com.jayway.jsonpath.PathNotFoundException;
2727
import com.jayway.jsonpath.Predicate;
28-
import org.junit.BeforeClass;
29-
import org.junit.Test;
30-
import org.junit.runner.RunWith;
28+
import org.junit.jupiter.api.BeforeAll;
29+
import org.junit.jupiter.api.Test;
3130

3231
import org.springframework.beans.factory.annotation.Autowired;
3332
import org.springframework.context.annotation.Bean;
@@ -39,28 +38,25 @@
3938
import org.springframework.integration.channel.QueueChannel;
4039
import org.springframework.integration.config.EnableIntegration;
4140
import org.springframework.integration.support.MessageBuilder;
41+
import org.springframework.integration.transformer.MessageTransformationException;
4242
import org.springframework.messaging.Message;
4343
import org.springframework.messaging.MessageChannel;
4444
import org.springframework.messaging.MessageHeaders;
4545
import org.springframework.messaging.PollableChannel;
4646
import org.springframework.messaging.handler.annotation.Payload;
4747
import org.springframework.messaging.support.GenericMessage;
48-
import org.springframework.test.context.ContextConfiguration;
49-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
50-
import org.springframework.test.context.support.AnnotationConfigContextLoader;
48+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
5149

5250
import static org.assertj.core.api.Assertions.assertThat;
53-
import static org.assertj.core.api.Assertions.fail;
51+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5452

5553
/**
5654
* @author Artem Bilan
5755
* @author Gary Russell
5856
*
5957
* @since 3.0
6058
*/
61-
@ContextConfiguration(classes = JsonPathTests.JsonPathTestsContextConfiguration.class,
62-
loader = AnnotationConfigContextLoader.class)
63-
@RunWith(SpringJUnit4ClassRunner.class)
59+
@SpringJUnitConfig(classes = JsonPathTests.JsonPathTestsContextConfiguration.class)
6460
public class JsonPathTests {
6561

6662
private static File JSON_FILE;
@@ -69,7 +65,7 @@ public class JsonPathTests {
6965

7066
private static Message<String> testMessage;
7167

72-
@BeforeClass
68+
@BeforeAll
7369
public static void setUp() throws IOException {
7470
ClassPathResource jsonResource = new ClassPathResource("JsonPathTests.json", JsonPathTests.class);
7571
JSON_FILE = jsonResource.getFile();
@@ -116,7 +112,7 @@ public static void setUp() throws IOException {
116112
private PollableChannel routerOutput2;
117113

118114
@Test
119-
public void testInt3139JsonPathTransformer() throws IOException {
115+
public void testInt3139JsonPathTransformer() {
120116
this.transformerInput.send(testMessage);
121117
Message<?> receive = this.output.receive(10000);
122118
assertThat(receive).isNotNull();
@@ -127,20 +123,14 @@ public void testInt3139JsonPathTransformer() throws IOException {
127123
assertThat(receive).isNotNull();
128124
assertThat(receive.getPayload()).isEqualTo("Nigel Rees");
129125

130-
this.transformerInput.send(new GenericMessage<File>(JSON_FILE));
126+
this.transformerInput.send(new GenericMessage<>(JSON_FILE));
131127
receive = this.output.receive(1000);
132128
assertThat(receive).isNotNull();
133129

134130
assertThat(receive.getPayload()).isEqualTo("Nigel Rees");
135-
try {
136-
this.transformerInput.send(new GenericMessage<Object>(new Object()));
137-
fail("IllegalArgumentException expected");
138-
}
139-
catch (Exception e) {
140-
//MessageTransformationException / MessageHandlingException / InvocationTargetException / IllegalArgumentException
141-
Throwable cause = e.getCause().getCause().getCause();
142-
assertThat(cause instanceof PathNotFoundException).isTrue();
143-
}
131+
assertThatExceptionOfType(MessageTransformationException.class)
132+
.isThrownBy(() -> this.transformerInput.send(new GenericMessage<>(new Object())))
133+
.withRootCauseInstanceOf(PathNotFoundException.class);
144134
}
145135

146136
@Test
@@ -165,13 +155,9 @@ public void testInt3139JsonPathFilter() {
165155
receive = this.output.receive(10000);
166156
assertThat(receive).isNotNull();
167157

168-
try {
169-
this.filterInput1.send(new GenericMessage<String>("{foo:{}}"));
170-
fail("MessageRejectedException is expected.");
171-
}
172-
catch (Exception e) {
173-
assertThat(e).isInstanceOf(MessageRejectedException.class);
174-
}
158+
assertThatExceptionOfType(MessageRejectedException.class)
159+
.isThrownBy(() -> this.filterInput1.send(new GenericMessage<>("{foo:{}}")));
160+
175161
receive = this.output.receive(0);
176162
assertThat(receive).isNull();
177163

@@ -186,7 +172,7 @@ public void testInt3139JsonPathSplitter() {
186172
for (int i = 0; i < 4; i++) {
187173
Message<?> receive = this.splitterOutput.receive(10000);
188174
assertThat(receive).isNotNull();
189-
assertThat(receive.getPayload() instanceof Map).isTrue();
175+
assertThat(receive.getPayload()).isInstanceOf(Map.class);
190176
}
191177
}
192178

0 commit comments

Comments
 (0)