3

I am trying to use TestSupportBinder to write tests for spring cloud stream application. All examples I found, including official documents and official github, are to test a Processor with a Transformer. These tests use Tranformer to get output Channel, and feed the Channel object into messageCollector.forChannel method to poll output.

However, instead of using the provided Processer, I wrote my own Interface. I can't find a way to get the required Channel object in test code. I had tried to autowire my Interface into test class to get Channel object but ended with java.lang.IllegalArgumentException: Channel [MY_CHANNEL] was not bound by class org.springframework.cloud.stream.test.binder.TestSupportBinder.

Community
  • 1
  • 1
ccshih
  • 1,190
  • 3
  • 17
  • 25

3 Answers3

1

The error message means that the channel MY_CHANNEL is not registered into MessageCollector. and, this indicates, your channel MY_CHANNEL from the interface wasn't declared with EnableBinding in your application. Do you have @EnableBinding(MyOwnInterface.class) in your application (with MyOwnInterface declaring the MY_CHANNEL channel)?

Ilayaperumal Gopinathan
  • 4,099
  • 1
  • 13
  • 12
  • Thanks for your answer. My application code is available in [another post]( http://stackoverflow.com/questions/43446135/spring-cloud-stream-kafka-dispatcher-has-no-subscribers-error). My test code is to vertify submitted request will get a message in "approved" channel. So, MY_CHANNEL is actually the "approved" channel. – ccshih Apr 17 '17 at 09:51
  • 4
    I find that `messageCollector. forChannel` method accepts `MessageChannel` but not `SubscribableChannel`. Passing a `SubscribableChannel` to `messageCollector. forChannel` leads to the exception. – ccshih Apr 19 '17 at 07:45
  • 4
    Along with the above, you can't register channels marked `@Input` with TestSupportBinder. Only channels marked `@Output` will work, and they must be of type MessageChannel in the binding interface. – Gabriel Bauman Feb 07 '20 at 03:42
1

After taking a look at the spring docs here I've noticed it seems that intentionally they only track the output channel, and not the input one.

For example, see:

For outbound message channels, the TestSupportBinder registers a single subscriber and retains the messages emitted by the application in a MessageCollector. They can be retrieved during tests and have assertions made against them.

Also, in the example they just demonstrate on the output channel:

messageCollector.forChannel(processor.output()).poll()

Seems to me that they expect you to track your input channel by the calls to the listening method.

orirab
  • 2,915
  • 1
  • 24
  • 48
0

It doesn't work for input channel. Only for output.

Not work: collector.forChannel(inputStream.inputStream()).clear();

public interface InputStream {
String INPUT_NAME = "offices";

@Input(INPUT_NAME)
SubscribableChannel inputStream();
}

Work: collector.forChannel(outputStream.outputStream()).clear();

public interface OutputStream {
String OUTPU_NAME = "offices";

@Output(OUTPUT_NAME)
SubscribableChannel outputStream();
}
Zhenya
  • 51
  • 6