【问题标题】:Junit for Spring Integration Components用于 Spring 集成组件的 Junit
【发布时间】:2018-05-11 18:16:49
【问题描述】:

我有以下用于调用 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 测试。需要建议。

【问题讨论】:

    标签: junit spring-integration spring-test


    【解决方案1】:

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

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

    不确定您为什么不期望从该网关方法返回,但无论如何这是您的逻辑。要处理responseChannel 中的回复,您可以考虑使用带有MockIntegration.mockMessageHandler()MockIntegrationContext.substituteMessageHandlerFor() 的Spring 集成测试框架,将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

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

    【讨论】:

    • 您好 Artem,很遗憾,我们无法升级到 5.0.5 版。能否请您指导我查看 4.3.9 版的 Spring 集成测试文档谢谢
    【解决方案2】:

    在 Artem 的回答的帮助下

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

    【讨论】:

      猜你喜欢
      • 2013-04-04
      • 2014-04-04
      • 2015-02-25
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多