用于弹簧集成组件的 Junit



我有以下配置设置来调用REST服务。

@Bean
public MessageChannel requestChannel() {
SubscribableChannel requestChannel= new DirectChannel();
return requestChannel;
}
@Bean
public MessageChannel responseChannel() {
SubscribableChannel responseChannel = new DirectChannel();
return responseChannel;
}
@Bean
@ServiceActivator(inputChannel = "requestChannel")
public MessageHandler httResponseMessageHandler(MessageChannel responseChannel,
HeaderMapper<HttpHeaders> headerMapper, RequestHandlerRetryAdvice retryAdvice) {
List<Advice> list = new ArrayList<>();
list.add(retryAdvice);
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("https://localhost:8080/myrest/service/");
handler.setHttpMethod(HttpMethod.POST);
handler.setHeaderMapper(headerMapper);
handler.setOutputChannel(responseChannel);
handler.setExpectedResponseType(RtpResponse.class);
handler.setAdviceChain(list);
return handler;
}

以下是网关和服务激活器发送请求和获取响应的配置。

@Override
@ServiceActivator(inputChannel="responseChannel")
public void receiveResponse(Message<RtpResponse> message) {
LOGGER.info("Message: " + message);
LOGGER.info("Message: " + message.getPayload());
LOGGER.info(message.getClass().getCanonicalName());
}
@Override
@Gateway(requestChannel = "requestChannel")
public void sendRequest(RtpRequestBody requestBody) {
requestChannel
.send(
MessageBuilder.withPayload(requestBody)
.setHeader("Accept","application/json")
.setHeader(MessageHeaders.CONTENT_TYPE,"application/json")
.setHeader(MessageHeaders.ERROR_CHANNEL,errorChannel)
.build()
);
}

我被困在如何为这个流程编写 Junit 上。我不确定我是否需要将此流程重组为 IntegrationFlow,这将有助于为此编写 Junit 测试。需要建议。

不确定您遇到了什么问题,但您只需要为该sendRequest()获取一个网关 bean 并调用它:

@Autowired
MyGateway myGateway;
@Test
void myTest() {
this.myGateway.sendRequest(...);
}

不确定为什么您不期望从该网关方法返回,但无论如何这就是您的逻辑。为了处理responseChannel中的回复,您可以考虑使用Spring集成测试框架及其MockIntegration.mockMessageHandler()MockIntegrationContext.substituteMessageHandlerFor(),将receiveResponse()服务激活器替换为可以在测试用例中验证和断言的内容。

请参阅参考手册中的更多信息:https://docs.spring.io/spring-integration/docs/5.0.5.RELEASE/reference/html/testing.html#test-context

更新

很遗憾,我们无法升级到版本 5.0.5。您能否指导我阅读 4.3.9 版的 Spring 集成测试文档

不,没有这样的。这实际上是Spring Integration 5.0:https://docs.spring.io/spring-integration/docs/5.0.5.RELEASE/reference/html/whats-new.html 中的一个新功能。

不过,您可以在执行测试用例之前获取requestChannelbean 并调用其addInterceptor(ChannelInterceptor interceptor)。在ChannelInterceptor.perSend()中,您可以收到回复消息并进行验证。

在阿尔乔姆的回答的帮助下

@Autowired
private SubscribableChannel employeeGetMethodResponseChannel;
@Autowired
private EmployeeSearchService employeeSearchService;
@Mock
private RestTemplate restTemplateMock;
private EmployeeDetail employeeDetailInResponse;
private Employee employeeInRequest;
@Before
public void setUp() {
employeeInRequest = new Employee();
employeeInRequest.setEmployeeId(1L);
employeeDetailInResponse = new EmployeeDetail();
employeeDetailInResponse.setEmployeeId(1L);
}
@Test
public void testGetRequest() {
Mockito.when(restTemplateMock.postForObject("https://localhost:8080/myrest/service/", EmployeeDetail.class))
.thenReturn(employeeDetailInResponse);
this.employeeSearchService.employeeSearch(employeeInRequest);
}
@Test
public void testResponse() {
this.responseChannel.subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
Assert.assertTrue("Test failed", ((EmployeeDetail) message.getPayload()).getEmployeeId().equals(1L));
}
});
}

最新更新