GH-9996: Migrating tests to Junit Jupiter and Deprecate Junit4 utilities

Fixes: https://github.com/spring-projects/spring-integration/issues/9996

Signed-off-by: Jiandong Ma <jiandong.ma.cn@gmail.com>
This commit is contained in:
Jiandong
2025-06-09 23:46:42 +08:00
committed by GitHub
parent bf9d3910cb
commit d229cb4930
54 changed files with 396 additions and 287 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2024 the original author or authors.
* Copyright 2018-2025 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.
@@ -24,6 +24,7 @@ import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.springframework.integration.test.condition.LogLevels;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.test.util.TestUtils.LevelsContainer;
import org.springframework.util.Assert;
@@ -37,10 +38,13 @@ import org.springframework.util.ObjectUtils;
*
* @author Artem Bilan
* @author Gary Russell
* @author Jiandong Ma
*
* @since 5.0.1
*
* @deprecated since 7.0 in favor of {@link LogLevels}
*/
@Deprecated(since = "7.0", forRemoval = true)
public final class Log4j2LevelAdjuster implements MethodRule {
private final Class<?>[] classes;

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2002-2025 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.test.support;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Convenience class for testing Spring Integration request-response message scenarios.
* Users create subclasses to execute on or more {@link RequestResponseScenario} tests.
* each scenario defines:
* <ul>
* <li>An inputChannelName</li>
* <li>An outputChannelName</li>
* <li>A payload or message to send as a request message on the inputChannel</li>
* <li>A handler to validate the response received on the outputChannel</li>
* </ul>
*
* @author David Turanski
* @author Gary Russell
* @author Artem Bilan
* @author Jiandong Ma
*/
@SpringJUnitConfig
@DirtiesContext
public abstract class AbstractRequestResponseScenarioTest {
private List<RequestResponseScenario> scenarios = null;
@Autowired
private ApplicationContext applicationContext;
@BeforeEach
public void setUp() {
scenarios = defineRequestResponseScenarios();
}
/**
* Execute each scenario. Instantiate the message channels, send the request message
* on the input channel and invoke the validator on the response received on the
* output channel. This can handle subscribable or pollable output channels.
*/
@Test
public void testRequestResponseScenarios() {
int i = 1;
for (RequestResponseScenario scenario : scenarios) {
String name = scenario.getName() == null ? "scenario-" + (i++) : scenario.getName();
scenario.init();
MessageChannel inputChannel = applicationContext.getBean(scenario.getInputChannelName(),
MessageChannel.class);
MessageChannel outputChannel = applicationContext.getBean(scenario.getOutputChannelName(),
MessageChannel.class);
if (outputChannel instanceof SubscribableChannel) {
((SubscribableChannel) outputChannel).subscribe(scenario.getResponseValidator());
}
assertThat(inputChannel.send(scenario.getMessage()))
.as(name + ": message not sent on " + scenario.getInputChannelName()).isTrue();
if (outputChannel instanceof PollableChannel) {
Message<?> response = ((PollableChannel) outputChannel).receive(10000); // NOSONAR magic number
assertThat(response).as(name + ": receive timeout on " + scenario.getOutputChannelName()).isNotNull();
scenario.getResponseValidator().handleMessage(response);
}
assertThat(scenario.getResponseValidator().getLastMessage())
.as("message was not handled on " + outputChannel + " for scenario '" + name + "'.").isNotNull();
if (outputChannel instanceof SubscribableChannel) {
((SubscribableChannel) outputChannel).unsubscribe(scenario.getResponseValidator());
}
}
}
/**
* Implement this method to define RequestResponse scenarios
* @return - A List of {@link RequestResponseScenario}
*/
protected abstract List<RequestResponseScenario> defineRequestResponseScenarios();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@@ -47,9 +47,13 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author David Turanski
* @author Gary Russell
* @author Artem Bilan
* @author Jiandong Ma
*
* @deprecated since 7.0 in favor of {@link AbstractRequestResponseScenarioTest}
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@Deprecated(since = "7.0", forRemoval = true)
public abstract class AbstractRequestResponseScenarioTests {
private List<RequestResponseScenario> scenarios = null;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2025 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.
@@ -23,16 +23,21 @@ import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.springframework.integration.test.condition.LongRunningTest;
/**
* Rule to prevent long running tests from running on every build; set environment
* variable RUN_LONG_INTEGRATION_TESTS on a CI nightly build to ensure coverage.
*
* @author Gary Russell
* @author Artem Bilan
* @author Jiandong Ma
*
* @since 3.0
*
* @deprecated since 7.0 in favor of {@link LongRunningTest}.
*/
@Deprecated(since = "7.0", forRemoval = true)
public class LongRunningIntegrationTest extends TestWatcher {
private static final Log LOGGER = LogFactory.getLog(LongRunningIntegrationTest.class);

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2002-2025 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.test.support;
import java.util.Collections;
import java.util.List;
/**
* Convenience class for a single {@link RequestResponseScenario} test
*
* @author David Turanski
* @author Jiandong Ma
*
*/
public abstract class SingleRequestResponseScenarioTest extends AbstractRequestResponseScenarioTest {
@Override
protected List<RequestResponseScenario> defineRequestResponseScenarios() {
return Collections.singletonList(defineRequestResponseScenario());
}
protected abstract RequestResponseScenario defineRequestResponseScenario();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 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.
@@ -23,7 +23,11 @@ import java.util.List;
* Convenience class for a single {@link RequestResponseScenario} test
*
* @author David Turanski
* @author Jiandong Ma
*
* @deprecated since 7.0 in favor of {@link SingleRequestResponseScenarioTest}
*/
@Deprecated(since = "7.0", forRemoval = true)
public abstract class SingleRequestResponseScenarioTests extends AbstractRequestResponseScenarioTests {
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2025 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,12 +19,9 @@ package org.springframework.integration.test.matcher;
import java.util.Date;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
@@ -33,6 +30,7 @@ import org.springframework.messaging.support.MessageBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -43,7 +41,6 @@ import static org.mockito.Mockito.when;
* @author Artem Bilan
*
*/
@RunWith(MockitoJUnitRunner.Silent.class)
public class MockitoMessageMatchersTests {
static final Date SOME_PAYLOAD = new Date();
@@ -52,15 +49,13 @@ public class MockitoMessageMatchersTests {
static final String SOME_HEADER_KEY = "test.foo";
@Mock
MessageHandler handler;
MessageHandler handler = mock();
@Mock
MessageChannel channel;
MessageChannel channel = mock();
Message<Date> message;
@Before
@BeforeEach
public void setUp() {
this.message =
MessageBuilder.withPayload(SOME_PAYLOAD)