ccshih
ccshih

Reputation: 1210

How to get channel in TestSupportBinder without using Transformer

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.

Upvotes: 3

Views: 4655

Answers (3)

Zhenya
Zhenya

Reputation: 79

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();
}

Upvotes: 0

orirab
orirab

Reputation: 3353

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.

Upvotes: 2

Ilayaperumal Gopinathan
Ilayaperumal Gopinathan

Reputation: 4179

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)?

Upvotes: 1

Related Questions