【问题标题】:How to test MessageChannel in Spring Integrtion?如何在 Spring Integration 中测试 MessageChannel?
【发布时间】:2021-06-03 13:20:24
【问题描述】:

我想知道消息是否通过特定渠道进行测试,或者我想从特定渠道获取消息

所以我的流程是:controller -> gateway -> ServiceActivator

    private final Gateway gateway;
    public ResponseEntity<Map<String,String>> submit(String applicationId, ApplicationDto applicationDto) {
    applicationDto.setApplicationId(applicationId);
    gateway.submitApplication(applicationDto);
    return ResponseEntity.ok(Map.of(MESSAGE, "Accepted submit"));
    }

网关

@Gateway(requestChannel = "submitApplicationChannel", replyChannel = "replySubmitApplicationChannel")
WorkflowPayload submitApplication(ApplicationDto applicationDto);

管道

@Bean
MessageChannel submitApplicationChannel() {
    return new DirectChannel();
}

所以我的测试是发送一个启动流程的请求

@Test
@DisplayName("Application Submission")
void submissionTest() throws Exception {
    
    mockMvc.perform(MockMvcRequestBuilders
            .post("/api/v1/applications/contract-validation/" + APPLICATION_ID)
            .contentType(MediaType.APPLICATION_JSON)
            .content(objectMapper.writeValueAsString(payload)))
            .andExpect(status().isAccepted())
            .andReturn();

//Check HERE if the message passed through the channel

}

你能帮帮我吗?

【问题讨论】:

    标签: spring unit-testing junit spring-integration


    【解决方案1】:

    在您的测试中,在调用网关之前将ChannelInterceptor 添加到submitApplicationChannel

    public interface ChannelInterceptor {
    
        /**
         * Invoked before the Message is actually sent to the channel.
         * This allows for modification of the Message if necessary.
         * If this method returns {@code null} then the actual
         * send invocation will not occur.
         */
        @Nullable
        default Message<?> preSend(Message<?> message, MessageChannel channel) {
            return message;
        }
    
        /**
         * Invoked immediately after the send invocation. The boolean
         * value argument represents the return value of that invocation.
         */
        default void postSend(Message<?> message, MessageChannel channel, boolean sent) {
        }
    
        /**
         * Invoked after the completion of a send regardless of any exception that
         * have been raised thus allowing for proper resource cleanup.
         * <p>Note that this will be invoked only if {@link #preSend} successfully
         * completed and returned a Message, i.e. it did not return {@code null}.
         * @since 4.1
         */
        default void afterSendCompletion(
                Message<?> message, MessageChannel channel, boolean sent, @Nullable Exception ex) {
        }
    
        /**
         * Invoked as soon as receive is called and before a Message is
         * actually retrieved. If the return value is 'false', then no
         * Message will be retrieved. This only applies to PollableChannels.
         */
        default boolean preReceive(MessageChannel channel) {
            return true;
        }
    
        /**
         * Invoked immediately after a Message has been retrieved but before
         * it is returned to the caller. The Message may be modified if
         * necessary; {@code null} aborts further interceptor invocations.
         * This only applies to PollableChannels.
         */
        @Nullable
        default Message<?> postReceive(Message<?> message, MessageChannel channel) {
            return message;
        }
    
        /**
         * Invoked after the completion of a receive regardless of any exception that
         * have been raised thus allowing for proper resource cleanup.
         * <p>Note that this will be invoked only if {@link #preReceive} successfully
         * completed and returned {@code true}.
         * @since 4.1
         */
        default void afterReceiveCompletion(@Nullable Message<?> message, MessageChannel channel,
                @Nullable Exception ex) {
        }
    
    }
    

    【讨论】:

    • 你有什么例子吗?
    • 只需在测试用例中@Autowired DirectChannel submitApplicationChannel,并在发送消息前为其添加拦截器,然后验证是否调用了拦截器。
    猜你喜欢
    • 2018-06-14
    • 2022-10-18
    • 2017-10-16
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 2018-12-24
    • 1970-01-01
    相关资源
    最近更新 更多