Skip to content

Commit b2a3d25

Browse files
committed
Migrate ReturnAddressTests to JUnit 5
Looks like `ApplicaitonContext.stop()` is note called on `close()`. This might cause an `OutOfMemoryError` on the CI * Use Spring context configuration for the test environment
1 parent e373fff commit b2a3d25

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed
Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -16,64 +16,63 @@
1616

1717
package org.springframework.integration.endpoint;
1818

19-
import org.junit.Test;
19+
import org.junit.jupiter.api.Test;
2020

21-
import org.springframework.context.support.ClassPathXmlApplicationContext;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.ApplicationContext;
2223
import org.springframework.integration.support.MessageBuilder;
2324
import org.springframework.messaging.Message;
2425
import org.springframework.messaging.MessageChannel;
2526
import org.springframework.messaging.MessagingException;
2627
import org.springframework.messaging.PollableChannel;
2728
import org.springframework.messaging.core.DestinationResolutionException;
2829
import org.springframework.messaging.support.GenericMessage;
30+
import org.springframework.test.annotation.DirtiesContext;
31+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2932

3033
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3135

3236
/**
3337
* @author Mark Fisher
3438
* @author Gary Russell
39+
* @author Artem Bilan
3540
*/
41+
@SpringJUnitConfig
42+
@DirtiesContext
3643
public class ReturnAddressTests {
3744

45+
@Autowired
46+
ApplicationContext context;
47+
3848
@Test
3949
public void returnAddressFallbackWithChannelReference() {
40-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
41-
"returnAddressTests.xml", this.getClass());
4250
MessageChannel channel3 = (MessageChannel) context.getBean("channel3");
4351
PollableChannel channel5 = (PollableChannel) context.getBean("channel5");
44-
context.start();
4552
Message<String> message = MessageBuilder.withPayload("*")
4653
.setReplyChannel(channel5).build();
4754
channel3.send(message);
4855
Message<?> response = channel5.receive(3000);
4956
assertThat(response).isNotNull();
5057
assertThat(response.getPayload()).isEqualTo("**");
51-
context.close();
5258
}
5359

5460
@Test
5561
public void returnAddressFallbackWithChannelName() {
56-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
57-
"returnAddressTests.xml", this.getClass());
5862
MessageChannel channel3 = (MessageChannel) context.getBean("channel3");
5963
PollableChannel channel5 = (PollableChannel) context.getBean("channel5");
60-
context.start();
6164
Message<String> message = MessageBuilder.withPayload("*")
6265
.setReplyChannelName("channel5").build();
6366
channel3.send(message);
6467
Message<?> response = channel5.receive(3000);
6568
assertThat(response).isNotNull();
6669
assertThat(response.getPayload()).isEqualTo("**");
67-
context.close();
6870
}
6971

7072
@Test
7173
public void returnAddressWithChannelReferenceAfterMultipleEndpoints() {
72-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
73-
"returnAddressTests.xml", this.getClass());
7474
MessageChannel channel1 = (MessageChannel) context.getBean("channel1");
7575
PollableChannel replyChannel = (PollableChannel) context.getBean("replyChannel");
76-
context.start();
7776
Message<String> message = MessageBuilder.withPayload("*")
7877
.setReplyChannel(replyChannel).build();
7978
channel1.send(message);
@@ -82,16 +81,12 @@ public void returnAddressWithChannelReferenceAfterMultipleEndpoints() {
8281
assertThat(response.getPayload()).isEqualTo("********");
8382
PollableChannel channel2 = (PollableChannel) context.getBean("channel2");
8483
assertThat(channel2.receive(0)).isNull();
85-
context.close();
8684
}
8785

8886
@Test
8987
public void returnAddressWithChannelNameAfterMultipleEndpoints() {
90-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
91-
"returnAddressTests.xml", this.getClass());
9288
MessageChannel channel1 = (MessageChannel) context.getBean("channel1");
9389
PollableChannel replyChannel = (PollableChannel) context.getBean("replyChannel");
94-
context.start();
9590
Message<String> message = MessageBuilder.withPayload("*")
9691
.setReplyChannelName("replyChannel").build();
9792
channel1.send(message);
@@ -100,47 +95,32 @@ public void returnAddressWithChannelNameAfterMultipleEndpoints() {
10095
assertThat(response.getPayload()).isEqualTo("********");
10196
PollableChannel channel2 = (PollableChannel) context.getBean("channel2");
10297
assertThat(channel2.receive(0)).isNull();
103-
context.close();
10498
}
10599

106100
@Test
107101
public void returnAddressFallbackButNotAvailable() {
108-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
109-
"returnAddressTests.xml", this.getClass());
110102
MessageChannel channel3 = (MessageChannel) context.getBean("channel3");
111-
context.start();
112-
GenericMessage<String> message = new GenericMessage<String>("*");
113-
try {
114-
channel3.send(message);
115-
}
116-
catch (MessagingException e) {
117-
assertThat(e.getCause() instanceof DestinationResolutionException).isTrue();
118-
}
119-
context.close();
103+
GenericMessage<String> message = new GenericMessage<>("*");
104+
assertThatExceptionOfType(MessagingException.class)
105+
.isThrownBy(() -> channel3.send(message))
106+
.withCauseInstanceOf(DestinationResolutionException.class);
120107
}
121108

122109
@Test
123110
public void outputChannelWithNoReturnAddress() {
124-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
125-
"returnAddressTests.xml", this.getClass());
126111
MessageChannel channel4 = (MessageChannel) context.getBean("channel4");
127112
PollableChannel replyChannel = (PollableChannel) context.getBean("replyChannel");
128-
context.start();
129-
GenericMessage<String> message = new GenericMessage<String>("*");
113+
GenericMessage<String> message = new GenericMessage<>("*");
130114
channel4.send(message);
131115
Message<?> response = replyChannel.receive(3000);
132116
assertThat(response).isNotNull();
133117
assertThat(response.getPayload()).isEqualTo("**");
134-
context.close();
135118
}
136119

137120
@Test
138121
public void outputChannelTakesPrecedence() {
139-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
140-
"returnAddressTests.xml", this.getClass());
141122
MessageChannel channel4 = (MessageChannel) context.getBean("channel4");
142123
PollableChannel replyChannel = (PollableChannel) context.getBean("replyChannel");
143-
context.start();
144124
Message<String> message = MessageBuilder.withPayload("*")
145125
.setReplyChannelName("channel5").build();
146126
channel4.send(message);
@@ -149,7 +129,6 @@ public void outputChannelTakesPrecedence() {
149129
assertThat(response.getPayload()).isEqualTo("**");
150130
PollableChannel channel5 = (PollableChannel) context.getBean("channel5");
151131
assertThat(channel5.receive(0)).isNull();
152-
context.close();
153132
}
154133

155134
}

0 commit comments

Comments
 (0)