Laures
Laures

Reputation: 5479

Testing spring-cloud-stream with their test-binder always throws MessageDeliveryException: Dispatcher has no subscribers for channel

I'm trying to write tests for my message producers. But whenever i try to send a message using the injected Bindings i get following error

Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers, failedMessage=GenericMessage [payload=..., headers={spanTraceId=0761b2d61b665041, spanId=e3b298682fefe198, spanParentSpanId=549ea87126d2484d, nativeHeaders={X-B3-TraceId=[0761b2d61b665041], spanTraceId=[0761b2d61b665041], X-B3-SpanId=[e3b298682fefe198], spanId=[e3b298682fefe198], X-B3-ParentSpanId=[549ea87126d2484d], spanParentSpanId=[549ea87126d2484d], X-B3-Sampled=[0], spanSampled=[0]}, X-B3-SpanId=e3b298682fefe198, X-B3-ParentSpanId=549ea87126d2484d, X-B3-Sampled=0, X-B3-TraceId=0761b2d61b665041, id=0c885a37-a1b1-fc0a-1314-e403ec1bffdb, spanSampled=0, contentType=application/json, timestamp=1589551815143}]
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:138)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
    ... 42 more

Since I'm testing the message producers there naturaly are no consumers in my code. How can i fix this exception?

Here is a minimal test class i created that shows the same behaviour:

@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = {MessagingTest.SampleConfiguration.class, Application.class, EventMockConfiguration.class})
public class MessagingTest {

    @Autowired
    private LibraryMessageSource libraryMessageSource;

    @Autowired
    private InputDestination input;

    @Autowired
    private OutputDestination output;

    @Test
    public void test() throws Exception {
        libraryMessageSource.creations().send(MessageBuilder.withPayload("test").build());
    }

    @SpringBootApplication
    @Import({TestChannelBinderConfiguration.class})
    public static class SampleConfiguration {

    }
}

Note: Application.class the main class for my app and EventMock overwrites a bean in the context with a specific mock.

public interface LibraryMessageSource {

    String LIBRARY_CREATE = "messages-library-create";
    String LIBRARY_UPDATE = "messages-library-update";

    @Input(LIBRARY_CREATE)
    MessageChannel creations();

    @Input(LIBRARY_UPDATE)
    MessageChannel updates();
}

Upvotes: 0

Views: 902

Answers (1)

Gary Russell
Gary Russell

Reputation: 174504

Since I'm testing the message producers

Your application has a consumer, not a producer

    @Input(INPUT)
    MessageChannel events();

and you have no @StreamListener subscribed to it.

    @Test
    public void test() throws Exception {
        libraryMessageSource.events().send(MessageBuilder.withPayload("test").build());
    }

Upvotes: 1

Related Questions