Java Spring Cloud Stream中通量中的契约测试消息给出IllegalArgumentException消息不能为null
我试图使用SpringCloudStream和SpringCloudContract对端点发出的事件流进行契约测试。但是,在运行测试时,我得到一个带有detailMessage的Java Spring Cloud Stream中通量中的契约测试消息给出IllegalArgumentException消息不能为null,java,spring,spring-boot,spring-cloud-stream,spring-cloud-contract,Java,Spring,Spring Boot,Spring Cloud Stream,Spring Cloud Contract,我试图使用SpringCloudStream和SpringCloudContract对端点发出的事件流进行契约测试。但是,在运行测试时,我得到一个带有detailMessage的IllegalArgumentException,消息不能为null。或者换句话说,我没有在我的消息接收器上收到任何消息。我的春季云版本是霍克斯顿。我在pom中添加了以下依赖项 <dependency> <groupId>org.springframework.cloud&
IllegalArgumentException
,消息不能为null。或者换句话说,我没有在我的消息接收器上收到任何消息。我的春季云版本是霍克斯顿。我在pom中添加了以下依赖项
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>
<!--TEST -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<type>test-jar</type>
<scope>test</scope>
<classifier>test-binder</classifier>
</dependency>
控制器
@PostMapping(value = "/")
public void handlePost() {
Faker faker = new Faker();
Message<String> message = MessageBuilder.withPayload(faker.chuckNorris().fact()).build();
// supplier.get();
EventSupplier.processor.onNext(message);
}
合同
org.springframework.cloud.contract.spec.Contract.make {
label 'some_label'
input {
triggeredBy("supply()")
}
outputMessage {
sentTo('supplier-out-0')
body(
anyNonBlankString()
)
headers {
messagingContentType(applicationJson())
}
}
}
我尝试过为函数和消费者创建契约,两者都奏效了。我还尝试使用String
类型的供应商,而不是Flux
类型的供应商,并且也成功了。您可以查看上面代码中的注释行
在调试过程中,我看到在StreamMessageCollectorMessageReceiver
中,在receive(“supplier-out-0”,5,“SECONDS”)
方法检索MessageChannel
bean时,我得到了一个directwithattributescannel
实例。另外,在查看文档时,我发现了一个MessageChannel
实现,在我看来,它似乎是应该实例化的候选实现
我猜spring cloud contract正在等待消息直接发送到队列中(DirectWithAttributesChannel
),但由于流量已经在队列中,我只是更新流量内容,这意味着没有消息被推送到队列中(FluxMessageChannel
?),因此没有收到任何消息(null
)这就是为什么我在运行测试时收到一条包含详细信息的IllegalArgumentException
消息不得为null
错误
我的配置中是否缺少一些东西?这个场景可以通过契约进行测试吗?使用契约进行测试是一种不好的方法吗?我看到了
@EnableBinding
注释,我假设它定义了一些通道等。重点是它是互斥的,您可以使用函数式或基于注释的bInDIN模型和通过绑定绑定有效地禁用了功能模块。考虑使用(显示完整的应用程序)作为一个起点。@OlegZhurakousky非常感谢您提供的链接,我缺少的是de@PollableBean
注释,而不是@Bean
。因此我想我的供应商没有被调查。我想有一些概念我可能还没有完全理解,我对我正在查看的示例感到困惑。无论如何,如果您愿意的话写下你的评论作为回答,我会将其标记为有效的。不,你的未命中配置与PollableBean无关。正如我所说的,EnableBinding将禁用所有功能配置。嗯,奇怪的是,在我将我的bean注释为@PollableBean
之前,我无法通过合同测试。无论如何,我删除了@Configuration
public class EventSupplier {
public static final EmitterProcessor<Message<String>> processor = EmitterProcessor.create();
@Bean
// public Supplier<String> supplier() {
public Supplier<Flux<Message<String>>> supplier() {
// return () -> "";
return () -> processor;
}
}
@ActiveProfiles("test")
@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMessageVerifier
@EnableBinding(KafkaStreamerApplication.class)
public abstract class KafkaStreamerApplicationTests {
@Autowired
private DummyController dummyController;
public void supply() {
dummyController.handlePost();
}
}
org.springframework.cloud.contract.spec.Contract.make {
label 'some_label'
input {
triggeredBy("supply()")
}
outputMessage {
sentTo('supplier-out-0')
body(
anyNonBlankString()
)
headers {
messagingContentType(applicationJson())
}
}
}