From da58fef3e01ffa635ade7df0d73854e1ac5c2aaf Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 22 Nov 2024 11:49:54 -0500 Subject: [PATCH] More `@DirtiesContext` for tests in `core` module --- .../DispatcherHasNoSubscribersTests.java | 57 +++--- .../channel/ExecutorChannelTests.java | 50 +++--- .../channel/FixedSubscriberChannelTests.java | 164 +++++------------- .../channel/PartitionedChannelTests.java | 2 + ...ctionSynchronizationQueueChannelTests.java | 21 +-- .../config/AutoGeneratedChannelTests.java | 14 +- .../ChannelCapacityPlaceholderTests.java | 13 +- .../channel/config/ChannelParserTests.java | 62 ++++--- .../ChannelWithCustomQueueParserTests.java | 18 +- .../config/DispatchingChannelParserTests.java | 6 +- .../message/MessageBuilderTests.java | 29 ++-- ...ropertiesPersistingMetadataStoreTests.java | 21 +-- 12 files changed, 187 insertions(+), 270 deletions(-) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/DispatcherHasNoSubscribersTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/DispatcherHasNoSubscribersTests.java index cda1f37a73..80c3177c6c 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/DispatcherHasNoSubscribersTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/DispatcherHasNoSubscribersTests.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,29 +16,28 @@ package org.springframework.integration.channel; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessagingException; import org.springframework.messaging.support.GenericMessage; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +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.fail; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Gary Russell * @author Artem Bilan + * * @since 2.1 * */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) +@SpringJUnitConfig +@DirtiesContext public class DispatcherHasNoSubscribersTests { @Autowired @@ -50,46 +49,32 @@ public class DispatcherHasNoSubscribersTests { @Autowired AbstractApplicationContext applicationContext; - @Before + @BeforeEach public void setup() { - applicationContext.setId("foo"); + applicationContext.setId("testApplicationId"); } @Test public void oneChannel() { - try { - noSubscribersChannel.send(new GenericMessage("Hello, world!")); - fail("Exception expected"); - } - catch (MessagingException e) { - assertThat(e.getMessage()) - .contains("Dispatcher has no subscribers for channel 'foo.noSubscribersChannel'."); - } + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(() -> noSubscribersChannel.send(new GenericMessage<>("Hello, world!"))) + .withMessageContaining("Dispatcher has no subscribers for channel 'testApplicationId.noSubscribersChannel'."); } @Test public void stackedChannels() { - try { - subscribedChannel.send(new GenericMessage("Hello, world!")); - fail("Exception expected"); - } - catch (MessagingException e) { - assertThat(e.getMessage()) - .contains("Dispatcher has no subscribers for channel 'foo.noSubscribersChannel'."); - } + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(() -> subscribedChannel.send(new GenericMessage<>("Hello, world!"))) + .withMessageContaining("Dispatcher has no subscribers for channel 'testApplicationId.noSubscribersChannel'."); } @Test public void withNoContext() { DirectChannel channel = new DirectChannel(); - channel.setBeanName("bar"); - try { - channel.send(new GenericMessage("Hello, world!")); - fail("Exception expected"); - } - catch (MessagingException e) { - assertThat(e.getMessage()).contains("Dispatcher has no subscribers for channel 'bar'."); - } + channel.setBeanName("testChannel"); + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(() -> channel.send(new GenericMessage("Hello, world!"))) + .withMessageContaining("Dispatcher has no subscribers for channel 'testChannel'."); } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/ExecutorChannelTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/ExecutorChannelTests.java index 5b380824d3..91c8638acd 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/ExecutorChannelTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/ExecutorChannelTests.java @@ -23,12 +23,14 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import org.junit.Test; +import org.assertj.core.api.InstanceOfAssertFactories; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.core.task.SyncTaskExecutor; import org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy; +import org.springframework.integration.util.ErrorHandlingTaskExecutor; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageDeliveryException; @@ -39,7 +41,7 @@ import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor; import org.springframework.scheduling.concurrent.CustomizableThreadFactory; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.BDDMockito.willThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -59,8 +61,7 @@ public class ExecutorChannelTests { TestHandler handler = new TestHandler(latch); channel.subscribe(handler); channel.send(new GenericMessage<>("test")); - latch.await(1000, TimeUnit.MILLISECONDS); - assertThat(latch.getCount()).isEqualTo(0); + assertThat(latch.await(10000, TimeUnit.MILLISECONDS)).isTrue(); assertThat(handler.thread).isNotNull(); assertThat(Thread.currentThread().equals(handler.thread)).isFalse(); assertThat(handler.thread.getName()).isEqualTo("test-1"); @@ -190,7 +191,11 @@ public class ExecutorChannelTests { @Test public void interceptorWithException() { - ExecutorChannel channel = new ExecutorChannel(new SyncTaskExecutor()); + QueueChannel errorChannel = new QueueChannel(); + MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler(); + errorHandler.setDefaultErrorChannel(errorChannel); + ErrorHandlingTaskExecutor executor = new ErrorHandlingTaskExecutor(new SyncTaskExecutor(), errorHandler); + ExecutorChannel channel = new ExecutorChannel(executor); channel.setBeanFactory(mock(BeanFactory.class)); channel.afterPropertiesSet(); @@ -202,12 +207,16 @@ public class ExecutorChannelTests { BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor(); channel.addInterceptor(interceptor); channel.subscribe(handler); - try { - channel.send(message); - } - catch (MessageDeliveryException actual) { - assertThat(actual.getCause()).isSameAs(expected); - } + channel.send(message); + + Message receive = errorChannel.receive(10000); + + assertThat(receive). + extracting(Message::getPayload) + .asInstanceOf(InstanceOfAssertFactories.throwable(MessageDeliveryException.class)) + .cause() + .isEqualTo(expected); + verify(handler).handleMessage(message); assertThat(interceptor.getCounter().get()).isEqualTo(1); assertThat(interceptor.wasAfterHandledInvoked()).isTrue(); @@ -216,17 +225,14 @@ public class ExecutorChannelTests { @Test public void testEarlySubscribe() { ExecutorChannel channel = new ExecutorChannel(mock(Executor.class)); - try { - channel.subscribe(m -> { - }); - channel.setBeanFactory(mock(BeanFactory.class)); - channel.afterPropertiesSet(); - fail("expected Exception"); - } - catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("You cannot subscribe() until the channel " - + "bean is fully initialized by the framework. Do not subscribe in a @Bean definition"); - } + channel.subscribe(m -> { + }); + channel.setBeanFactory(mock(BeanFactory.class)); + + assertThatIllegalStateException() + .isThrownBy(channel::afterPropertiesSet) + .withMessage("You cannot subscribe() until the channel " + + "bean is fully initialized by the framework. Do not subscribe in a @Bean definition"); } private static class TestHandler implements MessageHandler { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/FixedSubscriberChannelTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/FixedSubscriberChannelTests.java index 661276ea92..f758d31a9c 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/FixedSubscriberChannelTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/FixedSubscriberChannelTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2022 the original author or authors. + * Copyright 2014-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,32 +16,32 @@ package org.springframework.integration.channel; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.BeanInstantiationException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +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.fail; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Gary Russell + * @author Artem Bilan + * * @since 4.0 * */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) +@SpringJUnitConfig +@DirtiesContext public class FixedSubscriberChannelTests { @Autowired @@ -52,150 +52,76 @@ public class FixedSubscriberChannelTests { @Test public void testHappyDay() { - this.in.send(new GenericMessage("foo")); + this.in.send(new GenericMessage<>("test")); Message out = this.out.receive(0); - assertThat(out.getPayload()).isEqualTo("FOO"); + assertThat(out.getPayload()).isEqualTo("TEST"); assertThat(this.in).isInstanceOf(FixedSubscriberChannel.class); } @Test public void testNoSubs() { - ConfigurableApplicationContext context = null; - try { - context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "NoSubs-fail-context.xml", - this.getClass()); - fail("Expected exception"); - } - catch (Exception e) { - assertThat(e).isInstanceOf(BeanCreationException.class); - assertThat(e.getCause()).isInstanceOf(BeanInstantiationException.class); - assertThat(e.getCause().getCause()).isInstanceOf(IllegalArgumentException.class); - assertThat(e.getCause().getCause().getMessage()).contains("Cannot instantiate a"); - } - if (context != null) { - context.close(); - } + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "NoSubs-fail-context.xml", + this.getClass())) + .withCauseInstanceOf(BeanInstantiationException.class) + .withRootCauseInstanceOf(IllegalArgumentException.class) + .withStackTraceContaining("Cannot instantiate a"); } @Test public void testTwoSubs() { - ConfigurableApplicationContext context = null; - try { - context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "TwoSubs-fail-context.xml", - this.getClass()); - fail("Expected exception"); - } - catch (Exception e) { - assertThat(e).isInstanceOf(BeanDefinitionParsingException.class); - assertThat(e.getMessage()).contains("Only one subscriber is allowed for a FixedSubscriberChannel."); - } - if (context != null) { - context.close(); - } + assertThatExceptionOfType(BeanDefinitionParsingException.class) + .isThrownBy(() -> new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "TwoSubs-fail-context.xml", + this.getClass())) + .withMessageContaining("Only one subscriber is allowed for a FixedSubscriberChannel."); } @Test public void testTwoSubsAfter() { - ConfigurableApplicationContext context = null; - try { - context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "TwoSubsAfter-fail-context.xml", - this.getClass()); - fail("Expected exception"); - } - catch (Exception e) { - assertThat(e).isInstanceOf(IllegalArgumentException.class); - assertThat(e.getMessage()).contains("Only one subscriber is allowed for a FixedSubscriberChannel."); - } - if (context != null) { - context.close(); - } + assertThatExceptionOfType(BeanDefinitionParsingException.class) + .isThrownBy(() -> new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "TwoSubs-fail-context.xml", + this.getClass())) + .withMessageContaining("Only one subscriber is allowed for a FixedSubscriberChannel."); } @Test public void testInterceptors() { - ConfigurableApplicationContext context = null; - try { - context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "Interceptors-fail-context.xml", - this.getClass()); - fail("Expected exception"); - } - catch (Exception e) { - assertThat(e).isInstanceOf(BeanDefinitionParsingException.class); - assertThat(e.getMessage()).contains("Cannot have interceptors when 'fixed-subscriber=\"true\"'"); - } - if (context != null) { - context.close(); - } + assertThatExceptionOfType(BeanDefinitionParsingException.class) + .isThrownBy(() -> new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "Interceptors-fail-context.xml", + this.getClass())) + .withMessageContaining("Cannot have interceptors when 'fixed-subscriber=\"true\"'"); } @Test public void testDatatype() { - ConfigurableApplicationContext context = null; - try { - context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "Datatype-fail-context.xml", - this.getClass()); - fail("Expected exception"); - } - catch (Exception e) { - assertThat(e).isInstanceOf(BeanDefinitionParsingException.class); - assertThat(e.getMessage()).contains("Cannot have 'datatype' when 'fixed-subscriber=\"true\"'"); - } - if (context != null) { - context.close(); - } + assertThatExceptionOfType(BeanDefinitionParsingException.class) + .isThrownBy(() -> new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "Datatype-fail-context.xml", + this.getClass())) + .withMessageContaining("Cannot have 'datatype' when 'fixed-subscriber=\"true\"'"); } @Test public void testConverter() { - ConfigurableApplicationContext context = null; - try { - context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "Converter-fail-context.xml", - this.getClass()); - fail("Expected exception"); - } - catch (Exception e) { - assertThat(e).isInstanceOf(BeanDefinitionParsingException.class); - assertThat(e.getMessage()).contains("Cannot have 'message-converter' when 'fixed-subscriber=\"true\"'"); - } - if (context != null) { - context.close(); - } + assertThatExceptionOfType(BeanDefinitionParsingException.class) + .isThrownBy(() -> new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "Converter-fail-context.xml", + this.getClass())) + .withMessageContaining("Cannot have 'message-converter' when 'fixed-subscriber=\"true\"'"); } @Test public void testQueue() { - ConfigurableApplicationContext context = null; - try { - context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "Queue-fail-context.xml", - this.getClass()); - fail("Expected exception"); - } - catch (Exception e) { - assertThat(e).isInstanceOf(BeanDefinitionParsingException.class); - assertThat(e.getMessage()) - .contains("The 'fixed-subscriber' attribute is not allowed when a child element is present."); - } - if (context != null) { - context.close(); - } + assertThatExceptionOfType(BeanDefinitionParsingException.class) + .isThrownBy(() -> new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "Queue-fail-context.xml", + this.getClass())) + .withMessageContaining("The 'fixed-subscriber' attribute is not allowed when a child element is present."); } @Test public void testDispatcher() { - ConfigurableApplicationContext context = null; - try { - context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "Dispatcher-fail-context.xml", - this.getClass()); - fail("Expected exception"); - } - catch (Exception e) { - assertThat(e).isInstanceOf(BeanDefinitionParsingException.class); - assertThat(e.getMessage()) - .contains("The 'fixed-subscriber' attribute is not allowed when a child element is present."); - } - if (context != null) { - context.close(); - } + assertThatExceptionOfType(BeanDefinitionParsingException.class) + .isThrownBy(() -> new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "Dispatcher-fail-context.xml", + this.getClass())) + .withMessageContaining("The 'fixed-subscriber' attribute is not allowed when a child element is present."); } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/PartitionedChannelTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/PartitionedChannelTests.java index e286b51c28..6b96077fd9 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/PartitionedChannelTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/PartitionedChannelTests.java @@ -42,6 +42,7 @@ import org.springframework.messaging.MessageHandler; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.ExecutorChannelInterceptor; import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -55,6 +56,7 @@ import static org.mockito.Mockito.mock; * @since 6.1 */ @SpringJUnitConfig +@DirtiesContext public class PartitionedChannelTests { @Test diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/TransactionSynchronizationQueueChannelTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/TransactionSynchronizationQueueChannelTests.java index 3c79f45117..44b9ce875a 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/TransactionSynchronizationQueueChannelTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/TransactionSynchronizationQueueChannelTests.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,17 +16,15 @@ package org.springframework.integration.channel; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.support.MessageBuilder; import org.springframework.messaging.Message; import org.springframework.messaging.support.GenericMessage; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; @@ -37,8 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @since 2.2 * */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) +@SpringJUnitConfig @DirtiesContext public class TransactionSynchronizationQueueChannelTests { @@ -51,7 +48,7 @@ public class TransactionSynchronizationQueueChannelTests { @Autowired private QueueChannel queueChannel2; - @Before + @BeforeEach public void setup() { this.good.purge(null); this.queueChannel.purge(null); @@ -59,7 +56,7 @@ public class TransactionSynchronizationQueueChannelTests { } @Test - public void testCommit() throws Exception { + public void testCommit() { GenericMessage sentMessage = new GenericMessage<>("hello"); this.queueChannel.send(sentMessage); Message message = this.good.receive(10000); @@ -69,7 +66,7 @@ public class TransactionSynchronizationQueueChannelTests { } @Test - public void testRollback() throws Exception { + public void testRollback() { this.queueChannel.send(new GenericMessage<>("fail")); Message message = this.good.receive(10000); assertThat(message).isNotNull(); @@ -77,7 +74,7 @@ public class TransactionSynchronizationQueueChannelTests { } @Test - public void testIncludeChannelName() throws Exception { + public void testIncludeChannelName() { Message sentMessage = MessageBuilder.withPayload("hello") .setHeader("foo", "bar").build(); queueChannel2.send(sentMessage); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/config/AutoGeneratedChannelTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/config/AutoGeneratedChannelTests.java index 499e7b92b3..6eb65dff4f 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/config/AutoGeneratedChannelTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/config/AutoGeneratedChannelTests.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,22 +16,22 @@ package org.springframework.integration.channel.config; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.integration.channel.DirectChannel; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; /** * @author Mark Fisher + * @author Artem Bilan */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) +@SpringJUnitConfig +@DirtiesContext public class AutoGeneratedChannelTests { @Autowired diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelCapacityPlaceholderTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelCapacityPlaceholderTests.java index 01d6e73576..b234a82aeb 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelCapacityPlaceholderTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelCapacityPlaceholderTests.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,16 +16,15 @@ package org.springframework.integration.channel.config; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.integration.channel.PriorityChannel; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.support.MessageBuilder; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; @@ -33,8 +32,8 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Mark Fisher * @author Artem Bilan */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) +@SpringJUnitConfig +@DirtiesContext public class ChannelCapacityPlaceholderTests { @Autowired diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelParserTests.java index e43040927a..f10bf05ab9 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelParserTests.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. @@ -18,8 +18,7 @@ package org.springframework.integration.channel.config; import java.util.concurrent.Executor; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.FatalBeanException; @@ -48,46 +47,47 @@ import org.springframework.messaging.converter.MessageConverter; import org.springframework.messaging.support.ChannelInterceptor; import org.springframework.messaging.support.GenericMessage; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +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 Iwein Fuld * @author Gunnar Hillert * @author Gary Russell + * @author Artem Bilan * * @see ChannelWithCustomQueueParserTests */ -@ContextConfiguration(locations = { +@SpringJUnitConfig(locations = { "/org/springframework/integration/channel/config/ChannelParserTests-context.xml", "/org/springframework/integration/channel/config/priorityChannelParserTests.xml"}) -@RunWith(SpringJUnit4ClassRunner.class) @DirtiesContext public class ChannelParserTests { @Autowired private ApplicationContext context; - @Test(expected = FatalBeanException.class) + @Test public void testChannelWithoutId() { - new ClassPathXmlApplicationContext("channelWithoutId.xml", this.getClass()).close(); + assertThatExceptionOfType(FatalBeanException.class) + .isThrownBy(() -> new ClassPathXmlApplicationContext("channelWithoutId.xml", getClass())); } @Test public void testChannelWithCapacity() { MessageChannel channel = (MessageChannel) context.getBean("capacityChannel"); for (int i = 0; i < 10; i++) { - boolean result = channel.send(new GenericMessage("test"), 10); + boolean result = channel.send(new GenericMessage<>("test"), 10); assertThat(result).isTrue(); } - assertThat(channel.send(new GenericMessage("test"), 3)).isFalse(); + assertThat(channel.send(new GenericMessage<>("test"), 3)).isFalse(); } @Test - public void testDirectChannelByDefault() throws InterruptedException { + public void testDirectChannelByDefault() { MessageChannel channel = (MessageChannel) context.getBean("defaultChannel"); assertThat(channel).isInstanceOf(DirectChannel.class); DirectFieldAccessor accessor = new DirectFieldAccessor(channel); @@ -98,7 +98,7 @@ public class ChannelParserTests { } @Test - public void testExecutorChannel() throws InterruptedException { + public void testExecutorChannel() { MessageChannel channel = context.getBean("executorChannel", MessageChannel.class); assertThat(channel).isInstanceOf(ExecutorChannel.class); assertThat(TestUtils.getPropertyValue(channel, "messageConverter")).isNotNull(); @@ -106,7 +106,7 @@ public class ChannelParserTests { } @Test - public void testExecutorChannelNoConverter() throws InterruptedException { + public void testExecutorChannelNoConverter() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "ChannelParserTests-no-converter-context.xml", this.getClass()); MessageChannel channel = context.getBean("executorChannel", MessageChannel.class); @@ -117,7 +117,7 @@ public class ChannelParserTests { } @Test - public void channelWithFailoverDispatcherAttribute() throws Exception { + public void channelWithFailoverDispatcherAttribute() { MessageChannel channel = (MessageChannel) context.getBean("channelWithFailover"); assertThat(channel.getClass()).isEqualTo(DirectChannel.class); DirectFieldAccessor accessor = new DirectFieldAccessor(channel); @@ -127,13 +127,13 @@ public class ChannelParserTests { } @Test - public void testPublishSubscribeChannel() throws InterruptedException { + public void testPublishSubscribeChannel() { MessageChannel channel = (MessageChannel) context.getBean("publishSubscribeChannel"); assertThat(channel.getClass()).isEqualTo(PublishSubscribeChannel.class); } @Test - public void testPublishSubscribeChannelWithTaskExecutorReference() throws InterruptedException { + public void testPublishSubscribeChannelWithTaskExecutorReference() { MessageChannel channel = (MessageChannel) context.getBean("publishSubscribeChannelWithTaskExecutorRef"); assertThat(channel.getClass()).isEqualTo(PublishSubscribeChannel.class); DirectFieldAccessor accessor = new DirectFieldAccessor(channel); @@ -162,11 +162,12 @@ public class ChannelParserTests { assertThat(channel.send(new GenericMessage(123))).isTrue(); } - @Test(expected = MessageDeliveryException.class) + @Test public void testDatatypeChannelWithIncorrectType() { MessageChannel channel = (MessageChannel) context.getBean("integerChannel"); - channel.send(new GenericMessage("incorrect type")); assertThat(TestUtils.getPropertyValue(channel, "messageConverter") instanceof UselessMessageConverter).isTrue(); + assertThatExceptionOfType(MessageDeliveryException.class) + .isThrownBy(() -> channel.send(new GenericMessage<>("incorrect type"))); } @Test @@ -196,10 +197,11 @@ public class ChannelParserTests { assertThat(channel.send(new GenericMessage<>("accepted type"))).isTrue(); } - @Test(expected = MessageDeliveryException.class) + @Test public void testMultipleDatatypeChannelWithIncorrectType() { MessageChannel channel = (MessageChannel) context.getBean("stringOrNumberChannel"); - channel.send(new GenericMessage<>(Boolean.TRUE)); + assertThatExceptionOfType(MessageDeliveryException.class) + .isThrownBy(() -> channel.send(new GenericMessage<>(Boolean.TRUE))); } @Test @@ -218,11 +220,11 @@ public class ChannelParserTests { } @Test - public void testChannelInteceptorInnerBean() { + public void testChannelInterceptorInnerBean() { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("channelInterceptorParserTests.xml", getClass()); PollableChannel channel = (PollableChannel) context.getBean("channelWithInterceptorInnerBean"); - channel.send(new GenericMessage("test")); + channel.send(new GenericMessage<>("test")); Message transformed = channel.receive(1000); assertThat(transformed.getPayload()).isEqualTo("TEST"); context.close(); @@ -271,15 +273,11 @@ public class ChannelParserTests { assertThat(channel.receive(0).getPayload()).isEqualTo(1); assertThat(channel.receive(0).getPayload()).isEqualTo(2); assertThat(channel.receive(0).getPayload()).isEqualTo(3); - boolean threwException = false; - try { - channel.send(new GenericMessage<>("wrong type")); - } - catch (MessageDeliveryException e) { - assertThat(e.getFailedMessage().getPayload()).isEqualTo("wrong type"); - threwException = true; - } - assertThat(threwException).isTrue(); + + assertThatExceptionOfType(MessageDeliveryException.class) + .isThrownBy(() -> channel.send(new GenericMessage<>("wrong type"))) + .extracting("failedMessage.payload") + .isEqualTo("wrong type"); } public static class TestInterceptor implements ChannelInterceptor { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelWithCustomQueueParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelWithCustomQueueParserTests.java index 9399e5b7bc..ce2d4ce3b0 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelWithCustomQueueParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/config/ChannelWithCustomQueueParserTests.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. @@ -19,15 +19,14 @@ package org.springframework.integration.channel.config; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.integration.channel.QueueChannel; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; @@ -38,11 +37,12 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Iwein Fuld * @author Gunnar Hillert * @author Gary Russell + * @author Artem Bilan * * @see ChannelWithCustomQueueParserTests */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration +@SpringJUnitConfig +@DirtiesContext public class ChannelWithCustomQueueParserTests { @Qualifier("customQueueChannel") @@ -50,12 +50,12 @@ public class ChannelWithCustomQueueParserTests { QueueChannel customQueueChannel; @Test - public void parseConfig() throws Exception { + public void parseConfig() { assertThat(customQueueChannel).isNotNull(); } @Test - public void queueTypeSet() throws Exception { + public void queueTypeSet() { DirectFieldAccessor accessor = new DirectFieldAccessor(customQueueChannel); Object queue = accessor.getPropertyValue("queue"); assertThat(queue).isNotNull(); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/config/DispatchingChannelParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/config/DispatchingChannelParserTests.java index 8d7e31ff73..a7ad1d6b7c 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/config/DispatchingChannelParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/config/DispatchingChannelParserTests.java @@ -37,6 +37,7 @@ import org.springframework.integration.util.ErrorHandlingTaskExecutor; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; @@ -51,6 +52,7 @@ import static org.mockito.Mockito.mock; * @since 1.0.3 */ @SpringJUnitConfig +@DirtiesContext public class DispatchingChannelParserTests { @Autowired @@ -152,9 +154,7 @@ public class DispatchingChannelParserTests { } private static Object getDispatcherProperty(String propertyName, MessageChannel channel) { - return new DirectFieldAccessor( - new DirectFieldAccessor(channel).getPropertyValue("dispatcher")) - .getPropertyValue(propertyName); + return TestUtils.getPropertyValue(channel, "dispatcher." + propertyName); } public static class SampleLoadBalancingStrategy implements LoadBalancingStrategy { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/message/MessageBuilderTests.java b/spring-integration-core/src/test/java/org/springframework/integration/message/MessageBuilderTests.java index e9de78440d..751e6eb4ba 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/message/MessageBuilderTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/message/MessageBuilderTests.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. @@ -21,8 +21,7 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.IntegrationMessageHeaderAccessor; @@ -36,18 +35,19 @@ import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +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.assertThatIllegalArgumentException; /** * @author Mark Fisher * @author Gary Russell * @author Artem Bilan */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) +@SpringJUnitConfig +@DirtiesContext public class MessageBuilderTests { @Autowired @@ -59,9 +59,10 @@ public class MessageBuilderTests { @Autowired private MessageBuilderFactory messageBuilderFactory; - @Test(expected = IllegalArgumentException.class) // priority must be an Integer + @Test public void testPriorityHeader() { - MessageBuilder.withPayload("ha").setHeader("priority", "10").build(); + assertThatIllegalArgumentException() + .isThrownBy(() -> MessageBuilder.withPayload("ha").setHeader("priority", "10")); } @Test @@ -99,16 +100,18 @@ public class MessageBuilderTests { assertThat(message2.getHeaders().get("bar")).isEqualTo("2"); } - @Test(expected = IllegalArgumentException.class) + @Test public void testIdHeaderValueReadOnly() { UUID id = UUID.randomUUID(); - MessageBuilder.withPayload("test").setHeader(MessageHeaders.ID, id); + assertThatIllegalArgumentException() + .isThrownBy(() -> MessageBuilder.withPayload("test").setHeader(MessageHeaders.ID, id)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testTimestampValueReadOnly() { Long timestamp = 12345L; - MessageBuilder.withPayload("test").setHeader(MessageHeaders.TIMESTAMP, timestamp).build(); + assertThatIllegalArgumentException() + .isThrownBy(() -> MessageBuilder.withPayload("test").setHeader(MessageHeaders.TIMESTAMP, timestamp)); } @Test diff --git a/spring-integration-core/src/test/java/org/springframework/integration/metadata/PropertiesPersistingMetadataStoreTests.java b/spring-integration-core/src/test/java/org/springframework/integration/metadata/PropertiesPersistingMetadataStoreTests.java index e01733c73d..bb330c68f2 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/metadata/PropertiesPersistingMetadataStoreTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/metadata/PropertiesPersistingMetadataStoreTests.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. @@ -19,9 +19,8 @@ package org.springframework.integration.metadata; import java.io.File; import java.util.Properties; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.support.PropertiesLoaderUtils; @@ -33,12 +32,14 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Mark Fisher * @author Gunnar Hillert * @author Gary Russell + * @author Artem Bilan + * * @since 2.0 */ public class PropertiesPersistingMetadataStoreTests { - @Rule - public TemporaryFolder folder = new TemporaryFolder(); + @TempDir + static File folder; @Test public void validateWithDefaultBaseDir() throws Exception { @@ -61,9 +62,9 @@ public class PropertiesPersistingMetadataStoreTests { @Test public void validateWithCustomBaseDir() throws Exception { - File file = new File(this.folder.getRoot(), "metadata-store.properties"); + File file = new File(folder, "metadata-store.properties"); PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore(); - metadataStore.setBaseDirectory(folder.getRoot().getAbsolutePath()); + metadataStore.setBaseDirectory(folder.getAbsolutePath()); metadataStore.afterPropertiesSet(); metadataStore.put("foo", "bar"); metadataStore.close(); @@ -76,9 +77,9 @@ public class PropertiesPersistingMetadataStoreTests { @Test public void validateWithCustomFileName() throws Exception { - File file = new File(this.folder.getRoot(), "foo.properties"); + File file = new File(folder, "foo.properties"); PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore(); - metadataStore.setBaseDirectory(folder.getRoot().getAbsolutePath()); + metadataStore.setBaseDirectory(folder.getAbsolutePath()); metadataStore.setFileName("foo.properties"); metadataStore.afterPropertiesSet(); metadataStore.put("foo", "bar");