Skip to content

Commit cd910a1

Browse files
committed
Migrate event module tests to JUnit 5
* Use `@RecordApplicationEvents` feature from SF
1 parent c922905 commit cd910a1

File tree

6 files changed

+70
-98
lines changed

6 files changed

+70
-98
lines changed

spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,8 +22,7 @@
2222
import java.util.Properties;
2323
import java.util.Set;
2424

25-
import org.junit.Test;
26-
import org.junit.runner.RunWith;
25+
import org.junit.jupiter.api.Test;
2726

2827
import org.springframework.beans.DirectFieldAccessor;
2928
import org.springframework.beans.factory.annotation.Autowired;
@@ -41,7 +40,7 @@
4140
import org.springframework.messaging.PollableChannel;
4241
import org.springframework.test.context.ContextConfiguration;
4342
import org.springframework.test.context.TestExecutionListeners;
44-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
43+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4544
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
4645

4746
/**
@@ -53,7 +52,7 @@
5352
*
5453
* @since 2.0
5554
*/
56-
@RunWith(SpringJUnit4ClassRunner.class)
55+
@SpringJUnitConfig
5756
@ContextConfiguration
5857
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
5958
public class EventInboundChannelAdapterParserTests {

spring-integration-event/src/test/java/org/springframework/integration/event/config/EventOutboundChannelAdapterParserTests.java

+42-65
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,48 +18,59 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

21-
import java.util.concurrent.BrokenBarrierException;
22-
import java.util.concurrent.CyclicBarrier;
21+
import java.util.concurrent.CountDownLatch;
22+
import java.util.concurrent.TimeUnit;
2323

24-
import org.junit.Test;
25-
import org.junit.runner.RunWith;
24+
import org.junit.jupiter.api.AfterEach;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.Timeout;
2627

2728
import org.springframework.beans.DirectFieldAccessor;
2829
import org.springframework.beans.factory.annotation.Autowired;
2930
import org.springframework.context.ApplicationListener;
3031
import org.springframework.context.ConfigurableApplicationContext;
31-
import org.springframework.context.PayloadApplicationEvent;
3232
import org.springframework.context.support.ClassPathXmlApplicationContext;
3333
import org.springframework.integration.channel.DirectChannel;
3434
import org.springframework.integration.channel.QueueChannel;
3535
import org.springframework.integration.endpoint.EventDrivenConsumer;
36+
import org.springframework.integration.event.core.MessagingEvent;
3637
import org.springframework.integration.event.outbound.ApplicationEventPublishingMessageHandler;
3738
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
3839
import org.springframework.integration.test.util.TestUtils;
3940
import org.springframework.messaging.Message;
4041
import org.springframework.messaging.MessageHandler;
4142
import org.springframework.messaging.support.GenericMessage;
4243
import org.springframework.test.context.ContextConfiguration;
43-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44+
import org.springframework.test.context.event.ApplicationEvents;
45+
import org.springframework.test.context.event.RecordApplicationEvents;
46+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4447

4548
/**
4649
* @author Oleg Zhurakousky
4750
* @author Artem Bilan
4851
* @author Gary Russell
4952
* @author Gunnar Hillert
53+
*
5054
* @since 2.0
5155
*/
52-
@RunWith(SpringJUnit4ClassRunner.class)
56+
@SpringJUnitConfig
57+
@RecordApplicationEvents
5358
@ContextConfiguration
5459
public class EventOutboundChannelAdapterParserTests {
5560

5661
@Autowired
5762
private volatile ConfigurableApplicationContext context;
5863

59-
private volatile boolean receivedEvent;
64+
@Autowired
65+
private ApplicationEvents applicationEvents;
6066

6167
private static volatile int adviceCalled;
6268

69+
@AfterEach
70+
void cleanup() {
71+
this.applicationEvents.clear();
72+
}
73+
6374
@Test
6475
public void validateEventParser() {
6576
EventDrivenConsumer adapter = this.context.getBean("eventAdapter", EventDrivenConsumer.class);
@@ -73,88 +84,54 @@ public void validateEventParser() {
7384

7485
@Test
7586
public void validateUsage() {
76-
ApplicationListener<?> listener = event -> {
77-
if (event instanceof PayloadApplicationEvent) {
78-
String payload = (String) ((PayloadApplicationEvent<?>) event).getPayload();
79-
if (payload.equals("hello")) {
80-
receivedEvent = true;
81-
}
82-
}
83-
};
84-
this.context.addApplicationListener(listener);
8587
DirectChannel channel = context.getBean("input", DirectChannel.class);
86-
channel.send(new GenericMessage<String>("hello"));
87-
assertThat(this.receivedEvent).isTrue();
88+
channel.send(new GenericMessage<>("hello"));
89+
assertThat(this.applicationEvents.stream(String.class)).containsOnly("hello");
8890
}
8991

9092
@Test
9193
public void withAdvice() {
92-
this.receivedEvent = false;
93-
ApplicationListener<?> listener = event -> {
94-
Object source = event.getSource();
95-
if (source instanceof Message) {
96-
String payload = (String) ((Message<?>) source).getPayload();
97-
if (payload.equals("hello")) {
98-
receivedEvent = true;
99-
}
100-
}
101-
};
102-
context.addApplicationListener(listener);
10394
DirectChannel channel = context.getBean("inputAdvice", DirectChannel.class);
104-
channel.send(new GenericMessage<String>("hello"));
105-
assertThat(this.receivedEvent).isTrue();
95+
channel.send(new GenericMessage<>("hello"));
96+
assertThat(this.applicationEvents.stream(MessagingEvent.class))
97+
.hasSize(1)
98+
.satisfiesExactly(event -> assertThat(event.getMessage().getPayload()).isEqualTo("hello"));
10699
assertThat(adviceCalled).isEqualTo(1);
107100
}
108101

109-
@Test //INT-2275
102+
@Test
110103
public void testInsideChain() {
111-
this.receivedEvent = false;
112-
ApplicationListener<?> listener = event -> {
113-
Object source = event.getSource();
114-
if (source instanceof Message) {
115-
String payload = (String) ((Message<?>) source).getPayload();
116-
if (payload.equals("foobar")) {
117-
receivedEvent = true;
118-
}
119-
}
120-
};
121-
this.context.addApplicationListener(listener);
122104
DirectChannel channel = context.getBean("inputChain", DirectChannel.class);
123-
channel.send(new GenericMessage<String>("foo"));
124-
assertThat(this.receivedEvent).isTrue();
105+
channel.send(new GenericMessage<>("foo"));
106+
assertThat(this.applicationEvents.stream(MessagingEvent.class))
107+
.hasSize(1)
108+
.satisfiesExactly(event -> assertThat(event.getMessage().getPayload()).isEqualTo("foobar"));
125109
}
126110

127-
@Test(timeout = 10000)
128-
public void validateUsageWithPollableChannel() throws Exception {
129-
this.receivedEvent = false;
111+
@Test
112+
@Timeout(10000)
113+
public void validateUsageWithPollableChannel() throws InterruptedException {
130114
ConfigurableApplicationContext context =
131115
new ClassPathXmlApplicationContext("EventOutboundChannelAdapterParserTestsWithPollable-context.xml",
132116
EventOutboundChannelAdapterParserTests.class);
133-
final CyclicBarrier barrier = new CyclicBarrier(2);
134-
@SuppressWarnings("resource")
117+
118+
CountDownLatch eventLatch = new CountDownLatch(1);
119+
135120
ApplicationListener<?> listener = event -> {
136121
Object source = event.getSource();
137122
if (source instanceof Message) {
138123
String payload = (String) ((Message<?>) source).getPayload();
139124
if (payload.equals("hello")) {
140-
receivedEvent = true;
141-
try {
142-
barrier.await();
143-
}
144-
catch (InterruptedException e1) {
145-
Thread.currentThread().interrupt();
146-
}
147-
catch (BrokenBarrierException e2) {
148-
throw new IllegalStateException("broken barrier", e2);
149-
}
125+
eventLatch.countDown();
150126
}
151127
}
152128
};
153129
context.addApplicationListener(listener);
154130
QueueChannel channel = context.getBean("input", QueueChannel.class);
155-
channel.send(new GenericMessage<String>("hello"));
156-
barrier.await();
157-
assertThat(this.receivedEvent).isTrue();
131+
channel.send(new GenericMessage<>("hello"));
132+
133+
assertThat(eventLatch.await(10, TimeUnit.SECONDS)).isTrue();
134+
158135
context.close();
159136
}
160137

spring-integration-event/src/test/java/org/springframework/integration/event/dsl/IntegrationFlowEventsTests.java

+8-16
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

21-
import java.util.concurrent.atomic.AtomicReference;
22-
2321
import org.junit.jupiter.api.BeforeAll;
2422
import org.junit.jupiter.api.Test;
2523

@@ -46,6 +44,8 @@
4644
import org.springframework.messaging.PollableChannel;
4745
import org.springframework.messaging.support.GenericMessage;
4846
import org.springframework.test.annotation.DirtiesContext;
47+
import org.springframework.test.context.event.ApplicationEvents;
48+
import org.springframework.test.context.event.RecordApplicationEvents;
4949
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
5050

5151
/**
@@ -54,6 +54,7 @@
5454
* @since 5.0
5555
*/
5656
@SpringJUnitConfig
57+
@RecordApplicationEvents
5758
@DirtiesContext
5859
public class IntegrationFlowEventsTests {
5960

@@ -81,14 +82,15 @@ public static void setup() {
8182
private MessageChannel flow3Input;
8283

8384
@Autowired
84-
private AtomicReference<Object> eventHolder;
85+
private ApplicationEvents applicationEvents;
8586

8687
@Test
8788
public void testEventsFlow() {
88-
assertThat(this.eventHolder.get()).isNull();
89+
assertThat(this.applicationEvents.stream(MessagingEvent.class)).isEmpty();
8990
this.flow3Input.send(new GenericMessage<>("2"));
90-
assertThat(this.eventHolder.get()).isNotNull();
91-
assertThat(this.eventHolder.get()).isEqualTo(4);
91+
assertThat(this.applicationEvents.stream(MessagingEvent.class))
92+
.hasSize(1)
93+
.satisfiesExactly(event -> assertThat(event.getMessage().getPayload()).isEqualTo(4));
9294
}
9395

9496
@Test
@@ -118,16 +120,6 @@ public void testDelayRescheduling() {
118120
@EnableIntegration
119121
public static class ContextConfiguration {
120122

121-
@Bean
122-
public AtomicReference<Object> eventHolder() {
123-
return new AtomicReference<>();
124-
}
125-
126-
@Bean
127-
public ApplicationListener<MessagingEvent> eventListener() {
128-
return event -> eventHolder().set(event.getMessage().getPayload());
129-
}
130-
131123
@Bean
132124
public IntegrationFlow flow3() {
133125
return IntegrationFlow.from("flow3Input")

spring-integration-event/src/test/java/org/springframework/integration/event/inbound/ApplicationEventListeningMessageProducerTests.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,12 +17,13 @@
1717
package org.springframework.integration.event.inbound;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2021

2122
import java.util.Map;
2223
import java.util.Set;
2324
import java.util.concurrent.atomic.AtomicInteger;
2425

25-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
2627

2728
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2829
import org.springframework.context.ApplicationEvent;
@@ -193,7 +194,7 @@ public void messageAsSourceOrCustomEventType() {
193194
assertThat(message2.getPayload()).isEqualTo("test");
194195
}
195196

196-
@Test(expected = MessageHandlingException.class)
197+
@Test
197198
public void anyApplicationEventCausesExceptionWithErrorHandling() {
198199
DirectChannel channel = new DirectChannel();
199200
channel.subscribe(new AbstractReplyProducingMessageHandler() {
@@ -213,11 +214,12 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
213214
assertThat(message).isNotNull();
214215
assertThat(((Exception) message.getPayload()).getCause().getMessage()).isEqualTo("Failed");
215216
adapter.setErrorChannel(null);
216-
adapter.onApplicationEvent(new TestApplicationEvent1());
217+
assertThatExceptionOfType(MessageHandlingException.class)
218+
.isThrownBy(() -> adapter.onApplicationEvent(new TestApplicationEvent1()));
217219
}
218220

219221
@Test
220-
@SuppressWarnings({ "unchecked", "serial" })
222+
@SuppressWarnings("unchecked")
221223
public void testInt2935CheckRetrieverCache() {
222224
GenericApplicationContext ctx = TestUtils.createTestApplicationContext();
223225
ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();

spring-integration-event/src/test/java/org/springframework/integration/event/outbound/ApplicationEventPublishingMessageHandlerTests.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,7 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

21-
import org.junit.Test;
21+
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.context.ApplicationEvent;
2424
import org.springframework.context.ApplicationEventPublisher;
@@ -28,16 +28,17 @@
2828

2929
/**
3030
* @author Mark Fisher
31+
* @author Artem Bilan
3132
*/
3233
public class ApplicationEventPublishingMessageHandlerTests {
3334

3435
@Test
35-
public void messagingEvent() throws InterruptedException {
36+
public void messagingEvent() {
3637
TestApplicationEventPublisher publisher = new TestApplicationEventPublisher();
3738
ApplicationEventPublishingMessageHandler handler = new ApplicationEventPublishingMessageHandler();
3839
handler.setApplicationEventPublisher(publisher);
3940
assertThat(publisher.getLastEvent()).isNull();
40-
Message<?> message = new GenericMessage<String>("testing");
41+
Message<?> message = new GenericMessage<>("testing");
4142
handler.handleMessage(message);
4243
ApplicationEvent event = publisher.getLastEvent();
4344
assertThat(event.getClass()).isEqualTo(MessagingEvent.class);
@@ -50,11 +51,11 @@ public void payloadAsEvent() {
5051
ApplicationEventPublishingMessageHandler handler = new ApplicationEventPublishingMessageHandler();
5152
handler.setApplicationEventPublisher(publisher);
5253
assertThat(publisher.getLastEvent()).isNull();
53-
Message<?> message = new GenericMessage<TestEvent>(new TestEvent("foo"));
54+
Message<?> message = new GenericMessage<>(new TestEvent("foo"));
5455
handler.handleMessage(message);
5556
ApplicationEvent event = publisher.getLastEvent();
5657
assertThat(event.getClass()).isEqualTo(TestEvent.class);
57-
assertThat(((TestEvent) event).getSource()).isEqualTo("foo");
58+
assertThat((event).getSource()).isEqualTo("foo");
5859
}
5960

6061

@@ -85,6 +86,7 @@ private static class TestEvent extends ApplicationEvent {
8586
TestEvent(String text) {
8687
super(text);
8788
}
89+
8890
}
8991

9092
}

spring-integration-event/src/test/resources/log4j2-test.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</Appenders>
88
<Loggers>
99
<Logger name="org.springframework.integration" level="warn"/>
10-
<Logger name="org.springframework.integration.event" level="info"/>
10+
<Logger name="org.springframework.integration.event" level="warn"/>
1111
<Root level="warn">
1212
<AppenderRef ref="STDOUT" />
1313
</Root>

0 commit comments

Comments
 (0)