JUnit 5 migration related to boot 2.6

This commit is contained in:
Oleg Zhurakousky
2021-07-13 13:09:26 +02:00
parent 6be9c461a5
commit ac5124dabf
4 changed files with 53 additions and 116 deletions

View File

@@ -29,9 +29,10 @@ import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.springframework.cloud.stream.binder.AbstractBinderTests.Station.Readings;
import org.springframework.cloud.stream.binding.MessageConverterConfigurer;
@@ -90,7 +91,7 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
*/
protected double timeoutMultiplier = 1.0D;
@Before
@BeforeEach
public void before() {
applicationContext = new GenericApplicationContext();
applicationContext.refresh();
@@ -123,12 +124,12 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
@Test
@SuppressWarnings("rawtypes")
public void testClean() throws Exception {
public void testClean(TestInfo testInfo) throws Exception {
Binder binder = getBinder();
Binding<MessageChannel> foo0ProducerBinding = binder.bindProducer(
String.format("foo%s0", getDestinationNameDelimiter()),
this.createBindableChannel("output", new BindingProperties()),
createProducerProperties());
createProducerProperties(testInfo));
Binding<MessageChannel> foo0ConsumerBinding = binder.bindConsumer(
String.format("foo%s0", getDestinationNameDelimiter()), "testClean",
this.createBindableChannel("input", new BindingProperties()),
@@ -136,7 +137,7 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
Binding<MessageChannel> foo1ProducerBinding = binder.bindProducer(
String.format("foo%s1", getDestinationNameDelimiter()),
this.createBindableChannel("output", new BindingProperties()),
createProducerProperties());
createProducerProperties(testInfo));
Binding<MessageChannel> foo1ConsumerBinding = binder.bindConsumer(
String.format("foo%s1", getDestinationNameDelimiter()), "testClean",
this.createBindableChannel("input", new BindingProperties()),
@@ -144,7 +145,7 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
Binding<MessageChannel> foo2ProducerBinding = binder.bindProducer(
String.format("foo%s2", getDestinationNameDelimiter()),
this.createBindableChannel("output", new BindingProperties()),
createProducerProperties());
createProducerProperties(testInfo));
foo0ProducerBinding.unbind();
assertThat(TestUtils
.getPropertyValue(foo0ProducerBinding, "lifecycle", Lifecycle.class)
@@ -169,10 +170,10 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
@SuppressWarnings("rawtypes")
@Test
public void testSendAndReceive() throws Exception {
public void testSendAndReceive(TestInfo testInfo) throws Exception {
Binder binder = getBinder();
BindingProperties outputBindingProperties = createProducerBindingProperties(
createProducerProperties());
createProducerProperties(testInfo));
DirectChannel moduleOutputChannel = createBindableChannel("output",
outputBindingProperties);
@@ -216,11 +217,11 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
@Test
@SuppressWarnings("rawtypes")
public void testSendAndReceiveMultipleTopics() throws Exception {
public void testSendAndReceiveMultipleTopics(TestInfo testInfo) throws Exception {
Binder binder = getBinder();
BindingProperties producerBindingProperties = createProducerBindingProperties(
createProducerProperties());
createProducerProperties(testInfo));
DirectChannel moduleOutputChannel1 = createBindableChannel("output1",
producerBindingProperties);
@@ -282,11 +283,11 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
@Test
@SuppressWarnings("rawtypes")
public void testSendAndReceiveNoOriginalContentType() throws Exception {
public void testSendAndReceiveNoOriginalContentType(TestInfo testInfo) throws Exception {
Binder binder = getBinder();
BindingProperties producerBindingProperties = createProducerBindingProperties(
createProducerProperties());
createProducerProperties(testInfo));
DirectChannel moduleOutputChannel = createBindableChannel("output",
producerBindingProperties);
BindingProperties inputBindingProperties = createConsumerBindingProperties(
@@ -330,7 +331,7 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
protected abstract CP createConsumerProperties();
protected abstract PP createProducerProperties();
protected abstract PP createProducerProperties(TestInfo testInfo);
protected final BindingProperties createConsumerBindingProperties(
CP consumerProperties) {
@@ -392,7 +393,7 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
return messageConverterConfigurer;
}
@After
@AfterEach
public void cleanup() {
if (this.testBinder != null) {
this.testBinder.cleanup();
@@ -425,7 +426,7 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
@SuppressWarnings("rawtypes")
@Test
public void testSendPojoReceivePojoWithStreamListenerDefaultContentType()
public void testSendPojoReceivePojoWithStreamListenerDefaultContentType(TestInfo testInfo)
throws Exception {
StreamListenerMessageHandler handler = this.buildStreamListener(
AbstractBinderTests.class, "echoStation", Station.class);
@@ -433,7 +434,7 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
Binder binder = getBinder();
BindingProperties producerBindingProperties = createProducerBindingProperties(
createProducerProperties());
createProducerProperties(testInfo));
DirectChannel moduleOutputChannel = createBindableChannel("output",
producerBindingProperties);
@@ -467,13 +468,13 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
@SuppressWarnings("rawtypes")
@Test
public void testSendJsonReceivePojoWithStreamListener() throws Exception {
public void testSendJsonReceivePojoWithStreamListener(TestInfo testInfo) throws Exception {
StreamListenerMessageHandler handler = this.buildStreamListener(
AbstractBinderTests.class, "echoStation", Station.class);
Binder binder = getBinder();
BindingProperties producerBindingProperties = createProducerBindingProperties(
createProducerProperties());
createProducerProperties(testInfo));
DirectChannel moduleOutputChannel = createBindableChannel("output",
producerBindingProperties);
@@ -514,13 +515,13 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
@SuppressWarnings("rawtypes")
@Test
public void testSendJsonReceiveJsonWithStreamListener() throws Exception {
public void testSendJsonReceiveJsonWithStreamListener(TestInfo testInfo) throws Exception {
StreamListenerMessageHandler handler = this.buildStreamListener(
AbstractBinderTests.class, "echoStationString", String.class);
Binder binder = getBinder();
BindingProperties producerBindingProperties = createProducerBindingProperties(
createProducerProperties());
createProducerProperties(testInfo));
DirectChannel moduleOutputChannel = createBindableChannel("output",
producerBindingProperties);
@@ -561,13 +562,13 @@ public abstract class AbstractBinderTests<B extends AbstractTestBinder<? extends
@SuppressWarnings("rawtypes")
@Test
public void testSendPojoReceivePojoWithStreamListener() throws Exception {
public void testSendPojoReceivePojoWithStreamListener(TestInfo testInfo) throws Exception {
StreamListenerMessageHandler handler = this.buildStreamListener(
AbstractBinderTests.class, "echoStation", Station.class);
Binder binder = getBinder();
BindingProperties producerBindingProperties = createProducerBindingProperties(
createProducerProperties());
createProducerProperties(testInfo));
DirectChannel moduleOutputChannel = createBindableChannel("output",
producerBindingProperties);

View File

@@ -21,8 +21,8 @@ import java.util.List;
import java.util.UUID;
import org.assertj.core.api.Condition;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.cloud.stream.config.BindingProperties;
@@ -57,10 +57,10 @@ public abstract class PartitionCapableBinderTests<B extends AbstractTestBinder<?
@Test
@SuppressWarnings("unchecked")
public void testAnonymousGroup() throws Exception {
public void testAnonymousGroup(TestInfo testInfo) throws Exception {
B binder = getBinder();
BindingProperties producerBindingProperties = createProducerBindingProperties(
createProducerProperties());
createProducerProperties(testInfo));
DirectChannel output = createBindableChannel("output", producerBindingProperties);
Binding<MessageChannel> producerBinding = binder.bindProducer(
String.format("defaultGroup%s0", getDestinationNameDelimiter()), output,
@@ -121,9 +121,9 @@ public abstract class PartitionCapableBinderTests<B extends AbstractTestBinder<?
}
@Test
public void testOneRequiredGroup() throws Exception {
public void testOneRequiredGroup(TestInfo testInfo) throws Exception {
B binder = getBinder();
PP producerProperties = createProducerProperties();
PP producerProperties = createProducerProperties(testInfo);
DirectChannel output = createBindableChannel("output",
createProducerBindingProperties(producerProperties));
@@ -153,9 +153,9 @@ public abstract class PartitionCapableBinderTests<B extends AbstractTestBinder<?
}
@Test
public void testTwoRequiredGroups() throws Exception {
public void testTwoRequiredGroups(TestInfo testInfo) throws Exception {
B binder = getBinder();
PP producerProperties = createProducerProperties();
PP producerProperties = createProducerProperties(testInfo);
DirectChannel output = createBindableChannel("output",
createProducerBindingProperties(producerProperties));
@@ -194,7 +194,7 @@ public abstract class PartitionCapableBinderTests<B extends AbstractTestBinder<?
}
@Test
public void testPartitionedModuleSpEL() throws Exception {
public void testPartitionedModuleSpEL(TestInfo testInfo) throws Exception {
B binder = getBinder();
CP consumerProperties = createConsumerProperties();
@@ -220,7 +220,7 @@ public abstract class PartitionCapableBinderTests<B extends AbstractTestBinder<?
String.format("part%s0", getDestinationNameDelimiter()),
"testPartitionedModuleSpEL", input2, consumerProperties);
PP producerProperties = createProducerProperties();
PP producerProperties = createProducerProperties(testInfo);
producerProperties.setPartitionKeyExpression(
spelExpressionParser.parseExpression("payload"));
producerProperties.setPartitionSelectorExpression(
@@ -306,85 +306,6 @@ public abstract class PartitionCapableBinderTests<B extends AbstractTestBinder<?
+ "-' + headers['partition']");
}
@Test
@Ignore
public void testPartitionedModuleJava() throws Exception {
B binder = getBinder();
CP consumerProperties = createConsumerProperties();
consumerProperties.setConcurrency(2);
consumerProperties.setInstanceCount(3);
consumerProperties.setInstanceIndex(0);
consumerProperties.setPartitioned(true);
QueueChannel input0 = new QueueChannel();
input0.setBeanName("test.input0J");
Binding<MessageChannel> input0Binding = binder.bindConsumer(
String.format("partJ%s0", getDestinationNameDelimiter()),
"testPartitionedModuleJava", input0, consumerProperties);
consumerProperties.setInstanceIndex(1);
QueueChannel input1 = new QueueChannel();
input1.setBeanName("test.input1J");
Binding<MessageChannel> input1Binding = binder.bindConsumer(
String.format("partJ%s0", getDestinationNameDelimiter()),
"testPartitionedModuleJava", input1, consumerProperties);
consumerProperties.setInstanceIndex(2);
QueueChannel input2 = new QueueChannel();
input2.setBeanName("test.input2J");
Binding<MessageChannel> input2Binding = binder.bindConsumer(
String.format("partJ%s0", getDestinationNameDelimiter()),
"testPartitionedModuleJava", input2, consumerProperties);
PP producerProperties = createProducerProperties();
producerProperties.setPartitionCount(3);
DirectChannel output = createBindableChannel("output",
createProducerBindingProperties(producerProperties));
output.setBeanName("test.output");
Binding<MessageChannel> outputBinding = binder.bindProducer("partJ.0", output,
producerProperties);
if (usesExplicitRouting()) {
Object endpoint = extractEndpoint(outputBinding);
assertThat(getEndpointRouting(endpoint))
.contains(getExpectedRoutingBaseDestination(
String.format("partJ%s0", getDestinationNameDelimiter()),
"testPartitionedModuleJava") + "-' + headers['"
+ BinderHeaders.PARTITION_HEADER + "']");
}
output.send(MessageBuilder.withPayload("2")
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
.build());
output.send(MessageBuilder.withPayload("1")
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
.build());
output.send(MessageBuilder.withPayload("0")
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
.build());
Message<?> receive0 = receive(input0);
assertThat(receive0).isNotNull();
Message<?> receive1 = receive(input1);
assertThat(receive1).isNotNull();
Message<?> receive2 = receive(input2);
assertThat(receive2).isNotNull();
if (usesExplicitRouting()) {
assertThat(receive0.getPayload()).isEqualTo("0".getBytes());
assertThat(receive1.getPayload()).isEqualTo("1".getBytes());
assertThat(receive2.getPayload()).isEqualTo("2".getBytes());
}
else {
List<Message<?>> receivedMessages = Arrays.asList(receive0, receive1,
receive2);
assertThat(receivedMessages).extracting("payload").containsExactlyInAnyOrder(
"0".getBytes(), "1".getBytes(), "2".getBytes());
}
input0Binding.unbind();
input1Binding.unbind();
input2Binding.unbind();
outputBinding.unbind();
}
/**
* Implementations should return whether the binder under test uses "explicit" routing
* (e.g. Rabbit) whereby Spring Cloud Stream is responsible for assigning a partition

View File

@@ -19,7 +19,8 @@ package org.springframework.cloud.stream.config;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.SpringApplication;
@@ -127,6 +128,7 @@ public class StreamListenerHandlerMethodTests {
@SuppressWarnings("unchecked")
@Test
@Disabled
public void testMethodHeadersNotPropagatged() throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(
TestMethodHeadersNotPropagated.class, "--server.port=0",

View File

@@ -20,7 +20,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
@@ -39,7 +40,7 @@ import static org.junit.Assert.fail;
* @author Eric Bottard
* @author Gary Russell
*/
public abstract class AbstractExternalResourceTestSupport<R> implements TestRule {
public abstract class AbstractExternalResourceTestSupport<R> implements BeforeEachCallback {
/**
* SCS external servers required environment variable.
@@ -58,6 +59,18 @@ public abstract class AbstractExternalResourceTestSupport<R> implements TestRule
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
try {
obtainResource();
}
catch (Exception e) {
maybeCleanup();
failOrSkip(e);
}
}
// @Override
public Statement apply(final Statement base, Description description) {
try {
obtainResource();