如何在Spring集成中测试MessageChannel?

mu0hgdu0  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(198)

我想知道消息是否通过特定通道进行测试,或者我想从特定通道获取消息
所以我的流程是: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

}


你能给予我搭把手吗?

j5fpnvbx

j5fpnvbx1#

在测试中,在调用网关之前,将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) {
    }

}

字符串

相关问题