Skip to content

Commit

Permalink
More @DirtiesContext for tests in core module
Browse files Browse the repository at this point in the history
  • Loading branch information
artembilan committed Nov 22, 2024
1 parent 4ebe56e commit da58fef
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 270 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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<String>("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<String>("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<String>("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<String>("Hello, world!")))
.withMessageContaining("Dispatcher has no subscribers for channel 'testChannel'.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
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;
Expand All @@ -39,7 +41,7 @@
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;
Expand All @@ -59,8 +61,7 @@ public void verifyDifferentThread() throws Exception {
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");
Expand Down Expand Up @@ -190,7 +191,11 @@ public void interceptorWithModifiedMessage() {

@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();

Expand All @@ -202,12 +207,16 @@ public void interceptorWithException() {
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();
Expand All @@ -216,17 +225,14 @@ public void interceptorWithException() {
@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 {
Expand Down
Loading

0 comments on commit da58fef

Please sign in to comment.