gpt4 book ai didi

java - 使用云流测试不起作用

转载 作者:行者123 更新时间:2023-11-28 21:24:07 25 4
gpt4 key购买 nike

我用 spring-cloud-stream 尝试了一些东西。一切正常,现在我尝试编写一些测试用例。不幸的是他们不工作。我将所有内容简化为以下内容(所有内容都在同一个启动应用程序中):

发件人:

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

接收者:

@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;
}
}

我的配置:

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

测试:

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

我想使用 spring-cloud-stream-test-support 进行测试而不需要 AMQP 消息代理。在测试之外,我使用了 rabbitmq,一切正常。也许 spring-cloud-stream-test-support 并没有真正路由消息?或者这里的问题是什么?

最佳答案

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

正确;测试 Binder 只是一个线束,它不会在绑定(bind)之间路由;在同一个应用程序中为同一个目的地绑定(bind)生产者和消费者是不常见的。

当您在测试中发送消息时,您必须查询 Binder 以确保按预期发送。您可以使用 MessageCollector 来做到这一点。参见 the documentation您还可以查看一些 out of the box apps 的测试.

关于java - 使用云流测试不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44970573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com