Skip to content

Commit da58fef

Browse files
committed
More @DirtiesContext for tests in core module
1 parent 4ebe56e commit da58fef

12 files changed

+187
-270
lines changed
Lines changed: 21 additions & 36 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,29 +16,28 @@
1616

1717
package org.springframework.integration.channel;
1818

19-
import org.junit.Before;
20-
import org.junit.Test;
21-
import org.junit.runner.RunWith;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
2221

2322
import org.springframework.beans.factory.annotation.Autowired;
2423
import org.springframework.context.support.AbstractApplicationContext;
2524
import org.springframework.messaging.MessageChannel;
2625
import org.springframework.messaging.MessagingException;
2726
import org.springframework.messaging.support.GenericMessage;
28-
import org.springframework.test.context.ContextConfiguration;
29-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27+
import org.springframework.test.annotation.DirtiesContext;
28+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3029

31-
import static org.assertj.core.api.Assertions.assertThat;
32-
import static org.assertj.core.api.Assertions.fail;
30+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3331

3432
/**
3533
* @author Gary Russell
3634
* @author Artem Bilan
35+
*
3736
* @since 2.1
3837
*
3938
*/
40-
@ContextConfiguration
41-
@RunWith(SpringJUnit4ClassRunner.class)
39+
@SpringJUnitConfig
40+
@DirtiesContext
4241
public class DispatcherHasNoSubscribersTests {
4342

4443
@Autowired
@@ -50,46 +49,32 @@ public class DispatcherHasNoSubscribersTests {
5049
@Autowired
5150
AbstractApplicationContext applicationContext;
5251

53-
@Before
52+
@BeforeEach
5453
public void setup() {
55-
applicationContext.setId("foo");
54+
applicationContext.setId("testApplicationId");
5655
}
5756

5857
@Test
5958
public void oneChannel() {
60-
try {
61-
noSubscribersChannel.send(new GenericMessage<String>("Hello, world!"));
62-
fail("Exception expected");
63-
}
64-
catch (MessagingException e) {
65-
assertThat(e.getMessage())
66-
.contains("Dispatcher has no subscribers for channel 'foo.noSubscribersChannel'.");
67-
}
59+
assertThatExceptionOfType(MessagingException.class)
60+
.isThrownBy(() -> noSubscribersChannel.send(new GenericMessage<>("Hello, world!")))
61+
.withMessageContaining("Dispatcher has no subscribers for channel 'testApplicationId.noSubscribersChannel'.");
6862
}
6963

7064
@Test
7165
public void stackedChannels() {
72-
try {
73-
subscribedChannel.send(new GenericMessage<String>("Hello, world!"));
74-
fail("Exception expected");
75-
}
76-
catch (MessagingException e) {
77-
assertThat(e.getMessage())
78-
.contains("Dispatcher has no subscribers for channel 'foo.noSubscribersChannel'.");
79-
}
66+
assertThatExceptionOfType(MessagingException.class)
67+
.isThrownBy(() -> subscribedChannel.send(new GenericMessage<>("Hello, world!")))
68+
.withMessageContaining("Dispatcher has no subscribers for channel 'testApplicationId.noSubscribersChannel'.");
8069
}
8170

8271
@Test
8372
public void withNoContext() {
8473
DirectChannel channel = new DirectChannel();
85-
channel.setBeanName("bar");
86-
try {
87-
channel.send(new GenericMessage<String>("Hello, world!"));
88-
fail("Exception expected");
89-
}
90-
catch (MessagingException e) {
91-
assertThat(e.getMessage()).contains("Dispatcher has no subscribers for channel 'bar'.");
92-
}
74+
channel.setBeanName("testChannel");
75+
assertThatExceptionOfType(MessagingException.class)
76+
.isThrownBy(() -> channel.send(new GenericMessage<String>("Hello, world!")))
77+
.withMessageContaining("Dispatcher has no subscribers for channel 'testChannel'.");
9378
}
9479

9580
}

spring-integration-core/src/test/java/org/springframework/integration/channel/ExecutorChannelTests.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
import java.util.concurrent.TimeUnit;
2424
import java.util.concurrent.atomic.AtomicInteger;
2525

26-
import org.junit.Test;
26+
import org.assertj.core.api.InstanceOfAssertFactories;
27+
import org.junit.jupiter.api.Test;
2728

2829
import org.springframework.beans.factory.BeanFactory;
2930
import org.springframework.core.task.SimpleAsyncTaskExecutor;
3031
import org.springframework.core.task.SyncTaskExecutor;
3132
import org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy;
33+
import org.springframework.integration.util.ErrorHandlingTaskExecutor;
3234
import org.springframework.messaging.Message;
3335
import org.springframework.messaging.MessageChannel;
3436
import org.springframework.messaging.MessageDeliveryException;
@@ -39,7 +41,7 @@
3941
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
4042

4143
import static org.assertj.core.api.Assertions.assertThat;
42-
import static org.assertj.core.api.Assertions.fail;
44+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
4345
import static org.mockito.BDDMockito.willThrow;
4446
import static org.mockito.Mockito.mock;
4547
import static org.mockito.Mockito.verify;
@@ -59,8 +61,7 @@ public void verifyDifferentThread() throws Exception {
5961
TestHandler handler = new TestHandler(latch);
6062
channel.subscribe(handler);
6163
channel.send(new GenericMessage<>("test"));
62-
latch.await(1000, TimeUnit.MILLISECONDS);
63-
assertThat(latch.getCount()).isEqualTo(0);
64+
assertThat(latch.await(10000, TimeUnit.MILLISECONDS)).isTrue();
6465
assertThat(handler.thread).isNotNull();
6566
assertThat(Thread.currentThread().equals(handler.thread)).isFalse();
6667
assertThat(handler.thread.getName()).isEqualTo("test-1");
@@ -190,7 +191,11 @@ public void interceptorWithModifiedMessage() {
190191

191192
@Test
192193
public void interceptorWithException() {
193-
ExecutorChannel channel = new ExecutorChannel(new SyncTaskExecutor());
194+
QueueChannel errorChannel = new QueueChannel();
195+
MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
196+
errorHandler.setDefaultErrorChannel(errorChannel);
197+
ErrorHandlingTaskExecutor executor = new ErrorHandlingTaskExecutor(new SyncTaskExecutor(), errorHandler);
198+
ExecutorChannel channel = new ExecutorChannel(executor);
194199
channel.setBeanFactory(mock(BeanFactory.class));
195200
channel.afterPropertiesSet();
196201

@@ -202,12 +207,16 @@ public void interceptorWithException() {
202207
BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor();
203208
channel.addInterceptor(interceptor);
204209
channel.subscribe(handler);
205-
try {
206-
channel.send(message);
207-
}
208-
catch (MessageDeliveryException actual) {
209-
assertThat(actual.getCause()).isSameAs(expected);
210-
}
210+
channel.send(message);
211+
212+
Message<?> receive = errorChannel.receive(10000);
213+
214+
assertThat(receive).
215+
extracting(Message::getPayload)
216+
.asInstanceOf(InstanceOfAssertFactories.throwable(MessageDeliveryException.class))
217+
.cause()
218+
.isEqualTo(expected);
219+
211220
verify(handler).handleMessage(message);
212221
assertThat(interceptor.getCounter().get()).isEqualTo(1);
213222
assertThat(interceptor.wasAfterHandledInvoked()).isTrue();
@@ -216,17 +225,14 @@ public void interceptorWithException() {
216225
@Test
217226
public void testEarlySubscribe() {
218227
ExecutorChannel channel = new ExecutorChannel(mock(Executor.class));
219-
try {
220-
channel.subscribe(m -> {
221-
});
222-
channel.setBeanFactory(mock(BeanFactory.class));
223-
channel.afterPropertiesSet();
224-
fail("expected Exception");
225-
}
226-
catch (IllegalStateException e) {
227-
assertThat(e.getMessage()).isEqualTo("You cannot subscribe() until the channel "
228-
+ "bean is fully initialized by the framework. Do not subscribe in a @Bean definition");
229-
}
228+
channel.subscribe(m -> {
229+
});
230+
channel.setBeanFactory(mock(BeanFactory.class));
231+
232+
assertThatIllegalStateException()
233+
.isThrownBy(channel::afterPropertiesSet)
234+
.withMessage("You cannot subscribe() until the channel "
235+
+ "bean is fully initialized by the framework. Do not subscribe in a @Bean definition");
230236
}
231237

232238
private static class TestHandler implements MessageHandler {

0 commit comments

Comments
 (0)