Refactor some tests into JUnit 5

* Improve their interaction with Reactor Test
This commit is contained in:
Artem Bilan
2019-11-19 15:28:55 -05:00
parent 25e945908c
commit 0ec9859c0f
4 changed files with 70 additions and 90 deletions

View File

@@ -31,8 +31,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
@@ -51,7 +50,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import reactor.core.publisher.Flux;
@@ -62,7 +61,7 @@ import reactor.core.publisher.Flux;
*
* @since 5.0
*/
@RunWith(SpringRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class ReactiveStreamsTests {
@@ -98,7 +97,7 @@ public class ReactiveStreamsTests {
private Publisher<Message<String>> fixedSubscriberChannelFlow;
@Test
public void testReactiveFlow() throws Exception {
void testReactiveFlow() throws Exception {
List<String> results = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(6);
Flux.from(this.publisher)
@@ -115,7 +114,7 @@ public class ReactiveStreamsTests {
}
@Test
public void testPollableReactiveFlow() throws Exception {
void testPollableReactiveFlow() throws Exception {
this.inputChannel.send(new GenericMessage<>("1,2,3,4,5"));
CountDownLatch latch = new CountDownLatch(6);
@@ -152,7 +151,7 @@ public class ReactiveStreamsTests {
}
@Test
public void testFromPublisher() {
void testFromPublisher() {
Flux<Message<?>> messageFlux = Flux.just("1,2,3,4")
.map(v -> v.split(","))
.flatMapIterable(Arrays::asList)
@@ -178,11 +177,11 @@ public class ReactiveStreamsTests {
}
@Test
public void testFluxTransform() {
void testFluxTransform() {
QueueChannel resultChannel = new QueueChannel();
IntegrationFlow integrationFlow = f -> f
.split()
.split((splitter) -> splitter.delimiters(","))
.<String, String>fluxTransform(flux -> flux
.map(Message::getPayload)
.map(String::toUpperCase))
@@ -212,7 +211,7 @@ public class ReactiveStreamsTests {
}
@Test
public void singleChannelFlowTest() throws InterruptedException {
void singleChannelFlowTest() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
Flux.from(this.singleChannelFlow)
.map(m -> m.getPayload().toUpperCase())
@@ -224,7 +223,7 @@ public class ReactiveStreamsTests {
}
@Test
public void fixedSubscriberChannelFlowTest() throws InterruptedException {
void fixedSubscriberChannelFlowTest() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
Flux.from(this.fixedSubscriberChannelFlow)
.map(m -> m.getPayload().toUpperCase())

View File

@@ -18,13 +18,13 @@ package org.springframework.integration.splitter;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Test;
import org.reactivestreams.Subscriber;
import org.junit.jupiter.api.Test;
import org.springframework.integration.IntegrationMessageHeaderAccessor;
import org.springframework.integration.channel.DirectChannel;
@@ -32,7 +32,6 @@ import org.springframework.integration.channel.FluxMessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
@@ -45,10 +44,10 @@ import reactor.test.StepVerifier;
* @author Gunnar Hillert
* @author Artem Bilan
*/
public class DefaultSplitterTests {
class DefaultSplitterTests {
@Test
public void splitMessageWithArrayPayload() throws Exception {
void splitMessageWithArrayPayload() {
String[] payload = new String[] { "x", "y", "z" };
Message<String[]> message = MessageBuilder.withPayload(payload).build();
QueueChannel replyChannel = new QueueChannel();
@@ -69,7 +68,7 @@ public class DefaultSplitterTests {
}
@Test
public void splitMessageWithCollectionPayload() throws Exception {
void splitMessageWithCollectionPayload() {
List<String> payload = Arrays.asList("x", "y", "z");
Message<List<String>> message = MessageBuilder.withPayload(payload).build();
QueueChannel replyChannel = new QueueChannel();
@@ -90,7 +89,7 @@ public class DefaultSplitterTests {
}
@Test
public void correlationIdCopiedFromMessageId() {
void correlationIdCopiedFromMessageId() {
Message<String> message = MessageBuilder.withPayload("test").build();
DirectChannel inputChannel = new DirectChannel();
QueueChannel outputChannel = new QueueChannel(1);
@@ -105,7 +104,7 @@ public class DefaultSplitterTests {
}
@Test
public void splitMessageWithEmptyCollectionPayload() throws Exception {
void splitMessageWithEmptyCollectionPayload() {
Message<List<String>> message = MessageBuilder.withPayload(Collections.<String>emptyList()).build();
QueueChannel replyChannel = new QueueChannel();
DefaultMessageSplitter splitter = new DefaultMessageSplitter();
@@ -116,7 +115,7 @@ public class DefaultSplitterTests {
}
@Test
public void splitStream() {
void splitStream() {
Message<?> message = new GenericMessage<>(
Stream.generate(Math::random)
.limit(10));
@@ -133,7 +132,7 @@ public class DefaultSplitterTests {
}
@Test
public void splitFlux() {
void splitFlux() {
Message<?> message = new GenericMessage<>(
Flux
.generate(() -> 0,
@@ -159,7 +158,7 @@ public class DefaultSplitterTests {
}
@Test
public void splitArrayPayloadReactive() {
void splitArrayPayloadReactive() {
Message<?> message = new GenericMessage<>(new String[] { "x", "y", "z" });
FluxMessageChannel replyChannel = new FluxMessageChannel();
DefaultMessageSplitter splitter = new DefaultMessageSplitter();
@@ -174,14 +173,13 @@ public class DefaultSplitterTests {
StepVerifier.create(testFlux)
.expectNext("x", "y", "z")
.then(() ->
((Subscriber<?>) TestUtils.getPropertyValue(replyChannel, "subscribers", List.class).get(0))
.onComplete())
.verifyComplete();
.expectNoEvent(Duration.ofMillis(100))
.thenCancel()
.verify(Duration.ofSeconds(1));
}
@Test
public void splitStreamReactive() {
void splitStreamReactive() {
Message<?> message = new GenericMessage<>(Stream.of("x", "y", "z"));
FluxMessageChannel replyChannel = new FluxMessageChannel();
DefaultMessageSplitter splitter = new DefaultMessageSplitter();
@@ -196,14 +194,13 @@ public class DefaultSplitterTests {
StepVerifier.create(testFlux)
.expectNext("x", "y", "z")
.then(() ->
((Subscriber<?>) TestUtils.getPropertyValue(replyChannel, "subscribers", List.class).get(0))
.onComplete())
.verifyComplete();
.expectNoEvent(Duration.ofMillis(100))
.thenCancel()
.verify(Duration.ofSeconds(1));
}
@Test
public void splitFluxReactive() {
void splitFluxReactive() {
Message<?> message = new GenericMessage<>(Flux.just("x", "y", "z"));
FluxMessageChannel replyChannel = new FluxMessageChannel();
DefaultMessageSplitter splitter = new DefaultMessageSplitter();
@@ -218,10 +215,9 @@ public class DefaultSplitterTests {
StepVerifier.create(testFlux)
.expectNext("x", "y", "z")
.then(() ->
((Subscriber<?>) TestUtils.getPropertyValue(replyChannel, "subscribers", List.class).get(0))
.onComplete())
.verifyComplete();
.expectNoEvent(Duration.ofMillis(100))
.thenCancel()
.verify(Duration.ofSeconds(1));
}
}