Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class MockIntegrationContext implements BeanPostProcessor, SmartInitializ

private final Set<AbstractEndpoint> autoStartupCandidates = new HashSet<>();

@SuppressWarnings("NullAway.Init")
private ConfigurableListableBeanFactory beanFactory;

@Override
Expand Down Expand Up @@ -229,11 +230,14 @@ public void substituteMessageHandlerFor(String consumerEndpointId, // NOSONAR -
if (mockMessageHandler instanceof MessageProducer mockMessageProducer) {
if (targetMessageHandler instanceof MessageProducer messageProducer) {
MessageChannel outputChannel = messageProducer.getOutputChannel();
mockMessageProducer.setOutputChannel(outputChannel);
if (outputChannel != null) {
mockMessageProducer.setOutputChannel(outputChannel);
}
}
else {
if (mockMessageHandler instanceof MockMessageHandler) {
if (TestUtils.getPropertyValue(mockMessageHandler, "hasReplies", Boolean.class)) {
if (Boolean.TRUE.equals(TestUtils.getPropertyValue(
mockMessageHandler, "hasReplies", Boolean.class))) {
throw new IllegalStateException("The [" + mockMessageHandler + "] " +
"with replies can't replace simple MessageHandler [" + targetMessageHandler + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.util.List;

import org.jspecify.annotations.Nullable;

import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
Expand All @@ -38,7 +40,7 @@
class MockIntegrationContextCustomizerFactory implements ContextCustomizerFactory {

@Override
public ContextCustomizer createContextCustomizer(Class<?> testClass,
public @Nullable ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {

return TestContextAnnotationUtils.hasAnnotation(testClass, SpringIntegrationTest.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.integration.test.context;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.springframework.context.ApplicationContext;
Expand All @@ -37,7 +38,7 @@
*/
class SpringIntegrationTestExecutionListener implements TestExecutionListener {

private Set<AbstractEndpoint> autoStartupCandidates;
private Set<AbstractEndpoint> autoStartupCandidates = new HashSet<>();

@Override
public void prepareTestInstance(TestContext testContext) {
Expand All @@ -50,7 +51,8 @@ public void prepareTestInstance(TestContext testContext) {
MockIntegrationContext mockIntegrationContext = applicationContext.getBean(MockIntegrationContext.class);
this.autoStartupCandidates = mockIntegrationContext.getAutoStartupCandidates();
this.autoStartupCandidates.stream()
.filter(endpoint -> !match(endpoint.getBeanName(), patterns))
.filter(endpoint -> endpoint.getBeanName() != null
&& !match(endpoint.getBeanName(), patterns))
.peek(endpoint -> endpoint.setAutoStartup(true))
.forEach(AbstractEndpoint::start);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* Test context-related components.
*/
@org.jspecify.annotations.NullMarked
package org.springframework.integration.test.context;
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;

import org.jspecify.annotations.Nullable;
import org.mockito.ArgumentCaptor;
import org.mockito.BDDMockito;
import org.mockito.Mockito;
Expand Down Expand Up @@ -105,7 +106,7 @@ public static MessageSource<?> mockMessageSource(Message<?> message) {
* @see Mockito#mock(Class)
*/
@SuppressWarnings("rawtypes")
public static MessageSource<?> mockMessageSource(Message<?> message, Message<?>... messages) {
public static MessageSource<?> mockMessageSource(Message<?> message, Message<?> @Nullable... messages) {
MessageSource messageSource = Mockito.mock(MessageSource.class);

BDDMockito.given(messageSource.receive())
Expand All @@ -127,7 +128,7 @@ public static MockMessageHandler mockMessageHandler() {
* @param messageArgumentCaptor the Mockito ArgumentCaptor to capture incoming messages
* @return the MockMessageHandler instance ready for interaction
*/
public static MockMessageHandler mockMessageHandler(ArgumentCaptor<Message<?>> messageArgumentCaptor) {
public static MockMessageHandler mockMessageHandler(@Nullable ArgumentCaptor<Message<?>> messageArgumentCaptor) {
return Mockito.spy(new MockMessageHandler(messageArgumentCaptor));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import java.util.function.Consumer;
import java.util.function.Function;

import org.jspecify.annotations.Nullable;
import org.mockito.ArgumentCaptor;
import org.mockito.internal.matchers.CapturingMatcher;

import org.springframework.integration.handler.AbstractMessageProducingHandler;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.util.Assert;

/**
* The {@link AbstractMessageProducingHandler} extension for the mocking purpose in tests.
Expand Down Expand Up @@ -61,16 +63,16 @@ public class MockMessageHandler extends AbstractMessageProducingHandler {

private final Lock lock = new ReentrantLock();

protected final List<Function<Message<?>, ?>> messageFunctions = new LinkedList<>(); // NOSONAR final
protected final List<Function<Message<?>, ?>> messageFunctions = new LinkedList<>();

private final CapturingMatcher<Message<?>> capturingMatcher;
private final @Nullable CapturingMatcher<Message<?>> capturingMatcher;

protected Function<Message<?>, ?> lastFunction; // NOSONAR
protected @Nullable Function<Message<?>, ?> lastFunction;

protected boolean hasReplies; // NOSONAR
protected boolean hasReplies;

@SuppressWarnings("unchecked")
protected MockMessageHandler(ArgumentCaptor<Message<?>> messageArgumentCaptor) {
protected MockMessageHandler(@Nullable ArgumentCaptor<Message<?>> messageArgumentCaptor) {
if (messageArgumentCaptor != null) {
this.capturingMatcher = TestUtils.getPropertyValue(messageArgumentCaptor,
"capturingMatcher", CapturingMatcher.class);
Expand All @@ -85,6 +87,7 @@ protected MockMessageHandler(ArgumentCaptor<Message<?>> messageArgumentCaptor) {
* @param nextMessageConsumer the Consumer to handle the next incoming message.
* @return this
*/
@SuppressWarnings("NullAway") // See github.com/uber/NullAway/issues/1075
public MockMessageHandler handleNext(Consumer<Message<?>> nextMessageConsumer) {
this.lastFunction = m -> {
nextMessageConsumer.accept(m);
Expand Down Expand Up @@ -127,6 +130,7 @@ protected void handleMessageInternal(Message<?> message) {
this.lock.unlock();
}

Assert.notNull(function, "function must not be null");
Object result = function.apply(message);

if (result != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* Utilities for mocking integration components.
*/
@org.jspecify.annotations.NullMarked
package org.springframework.integration.test.mock;