user3046582
user3046582

Reputation: 341

Using Cloud Stream Test does not work

I tried some stuff with spring-cloud-stream. Everything works and now I tried to write some test cases. Unfortunately they are not working. I reduced everything to the following (Everything is in the same boot app):

The Sender:

@EnableBinding(Sender.Emitter.class)
public class Sender {

    public interface Emitter {
        String CHANNEL = "emitter";
        @Output(CHANNEL)
        MessageChannel events();
    }
    private Emitter emitter;

    public Sender(Emitter emitter) {
        this.emitter = emitter;
    }


    public void sendMessage(String massage) {
        emitter.events().send(MessageBuilder.withPayload(massage).build());
    }
}

The Receiver:

@EnableBinding(Receiver.Subscriber.class)
public class Receiver {
    public interface Subscriber {
        String CHANNEL = "subscriber";
        @Input(CHANNEL)
        SubscribableChannel events();
    }
    private String lastMessage;

    public String getLastMessage() {
        return lastMessage;
    }

    @StreamListener(Subscriber.CHANNEL)
    public void event(String message) {
        this.lastMessage = message;
    }
}

My config:

spring:
  cloud:
    stream:
      default-binder: rabbit
      bindings:
        emitter:
          destination: testtock
          content-type: application/json
        subscriber:
          destination: testtock

The Test:

@RunWith(SpringRunner.class)
@SpringBootTest
public class BasicTest {

    @Autowired
    private Receiver receiver;
    @Autowired
    private Sender sender;

    @Test
    public void test() throws InterruptedException {
        String massage = UUID.randomUUID().toString();
        sender.sendMessage(massage);
        //Thread.sleep(1000);
        assertEquals(massage, receiver.getLastMessage());
    }
}

I want use spring-cloud-stream-test-support for testing to not need a AMQP message broker. Outside of testing I use a rabbitmq, there everything is working. Maybe the spring-cloud-stream-test-support does not really route messages? Or what is the Problem here?

Upvotes: 0

Views: 2405

Answers (2)

Gary Russell
Gary Russell

Reputation: 174799

Maybe the spring-cloud-stream-test-support does not really route messages?

Correct; the test binder is just a harness, it doesn't route between bindings; it's unusual to have a producer and consumer binding for the same destination in the same app.

When you send a message in a test, you have to query the binder to ensure it was sent expected. You use a MessageCollector to do that. See the documentation and you can also look at the tests for some of the out of the box apps.

Upvotes: 1

Ilayaperumal Gopinathan
Ilayaperumal Gopinathan

Reputation: 4179

The spring-cloud-stream-test-support provides an ability to test individual Spring Cloud Stream application and uses TestSupportBinder. Hence, this is not meant for end-to-end integration testing like the one you are using above.

For more information on using spring-cloud-stream-test-support and the TestSupportBinder, you can refer the doc here

Upvotes: 0

Related Questions