From b2a3d25ffd7fdb75d9487caa1dbd8feef815d95f Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 16 Jul 2024 11:55:50 -0400 Subject: [PATCH] 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 --- ...sts.xml => ReturnAddressTests-context.xml} | 0 .../endpoint/ReturnAddressTests.java | 57 ++++++------------- 2 files changed, 18 insertions(+), 39 deletions(-) rename spring-integration-core/src/test/java/org/springframework/integration/endpoint/{returnAddressTests.xml => ReturnAddressTests-context.xml} (100%) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/returnAddressTests.xml b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ReturnAddressTests-context.xml similarity index 100% rename from spring-integration-core/src/test/java/org/springframework/integration/endpoint/returnAddressTests.xml rename to spring-integration-core/src/test/java/org/springframework/integration/endpoint/ReturnAddressTests-context.xml diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ReturnAddressTests.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ReturnAddressTests.java index 0c72e718a1..2a4d58cdf9 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ReturnAddressTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ReturnAddressTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,10 @@ package org.springframework.integration.endpoint; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; import org.springframework.integration.support.MessageBuilder; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; @@ -26,54 +27,52 @@ import org.springframework.messaging.MessagingException; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.core.DestinationResolutionException; import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Mark Fisher * @author Gary Russell + * @author Artem Bilan */ +@SpringJUnitConfig +@DirtiesContext public class ReturnAddressTests { + @Autowired + ApplicationContext context; + @Test public void returnAddressFallbackWithChannelReference() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "returnAddressTests.xml", this.getClass()); MessageChannel channel3 = (MessageChannel) context.getBean("channel3"); PollableChannel channel5 = (PollableChannel) context.getBean("channel5"); - context.start(); Message message = MessageBuilder.withPayload("*") .setReplyChannel(channel5).build(); channel3.send(message); Message response = channel5.receive(3000); assertThat(response).isNotNull(); assertThat(response.getPayload()).isEqualTo("**"); - context.close(); } @Test public void returnAddressFallbackWithChannelName() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "returnAddressTests.xml", this.getClass()); MessageChannel channel3 = (MessageChannel) context.getBean("channel3"); PollableChannel channel5 = (PollableChannel) context.getBean("channel5"); - context.start(); Message message = MessageBuilder.withPayload("*") .setReplyChannelName("channel5").build(); channel3.send(message); Message response = channel5.receive(3000); assertThat(response).isNotNull(); assertThat(response.getPayload()).isEqualTo("**"); - context.close(); } @Test public void returnAddressWithChannelReferenceAfterMultipleEndpoints() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "returnAddressTests.xml", this.getClass()); MessageChannel channel1 = (MessageChannel) context.getBean("channel1"); PollableChannel replyChannel = (PollableChannel) context.getBean("replyChannel"); - context.start(); Message message = MessageBuilder.withPayload("*") .setReplyChannel(replyChannel).build(); channel1.send(message); @@ -82,16 +81,12 @@ public class ReturnAddressTests { assertThat(response.getPayload()).isEqualTo("********"); PollableChannel channel2 = (PollableChannel) context.getBean("channel2"); assertThat(channel2.receive(0)).isNull(); - context.close(); } @Test public void returnAddressWithChannelNameAfterMultipleEndpoints() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "returnAddressTests.xml", this.getClass()); MessageChannel channel1 = (MessageChannel) context.getBean("channel1"); PollableChannel replyChannel = (PollableChannel) context.getBean("replyChannel"); - context.start(); Message message = MessageBuilder.withPayload("*") .setReplyChannelName("replyChannel").build(); channel1.send(message); @@ -100,47 +95,32 @@ public class ReturnAddressTests { assertThat(response.getPayload()).isEqualTo("********"); PollableChannel channel2 = (PollableChannel) context.getBean("channel2"); assertThat(channel2.receive(0)).isNull(); - context.close(); } @Test public void returnAddressFallbackButNotAvailable() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "returnAddressTests.xml", this.getClass()); MessageChannel channel3 = (MessageChannel) context.getBean("channel3"); - context.start(); - GenericMessage message = new GenericMessage("*"); - try { - channel3.send(message); - } - catch (MessagingException e) { - assertThat(e.getCause() instanceof DestinationResolutionException).isTrue(); - } - context.close(); + GenericMessage message = new GenericMessage<>("*"); + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(() -> channel3.send(message)) + .withCauseInstanceOf(DestinationResolutionException.class); } @Test public void outputChannelWithNoReturnAddress() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "returnAddressTests.xml", this.getClass()); MessageChannel channel4 = (MessageChannel) context.getBean("channel4"); PollableChannel replyChannel = (PollableChannel) context.getBean("replyChannel"); - context.start(); - GenericMessage message = new GenericMessage("*"); + GenericMessage message = new GenericMessage<>("*"); channel4.send(message); Message response = replyChannel.receive(3000); assertThat(response).isNotNull(); assertThat(response.getPayload()).isEqualTo("**"); - context.close(); } @Test public void outputChannelTakesPrecedence() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "returnAddressTests.xml", this.getClass()); MessageChannel channel4 = (MessageChannel) context.getBean("channel4"); PollableChannel replyChannel = (PollableChannel) context.getBean("replyChannel"); - context.start(); Message message = MessageBuilder.withPayload("*") .setReplyChannelName("channel5").build(); channel4.send(message); @@ -149,7 +129,6 @@ public class ReturnAddressTests { assertThat(response.getPayload()).isEqualTo("**"); PollableChannel channel5 = (PollableChannel) context.getBean("channel5"); assertThat(channel5.receive(0)).isNull(); - context.close(); } }