如何在IntegrationFlow组件上编写单元测试?

时间:2018-08-06 16:21:21

标签: java spring-integration spring-test spring-integration-dsl

我有几个@Configuration类,其中包括由IntegrationFlow注释的高度复杂的@Bean。我想对这些流程中使用的每个单个组件进行单元测试。例如:

@Configuration
public class FirstClass{

@Bean
public MessageChannel requestChannel() {
return MessageChannels.direct().get();
}

@Bean
public MessageChannel responseChannel() {
return MessageChannels.direct().get();
}

@Bean
public IntegrationFlow myFlow(MessageChannel requestChannel, MessageChannel responseChannel) {
return from(requestChannel)...transform(this::myFirstCustomTransformer)....transform(this::mySecondCustomTransformer)...get();
} 

//sorry for the continues dots, they indicate the existence of other components as well

private Map<String, Object> myFirstCustomTransformer(Map<String, Object> payload){
//do some stuff
}

private String mySecondCustomTransformer(Map<String, Object> payload) {
//do some stuff
}}

所以我想在单元测试中测试mySecondCustomTransformermyFirstCustomTransformer。我该如何实现?哪个是最佳解决方案?

* UPDATE

这些转换器包括对方法的调用,这些方法也是同一类的成员。因此,还必须为其余方法开发单元测试。

1 个答案:

答案 0 :(得分:2)

ActualObject创建了一个消费者终结点组件,该组件可以通过其function isEqual (ExpectedObject, ActualObject) { // if the ExpectedObject has a different number of // keys than ActualObje, it is not equal if (Object.keys(ExpectedObject).length !== Object.keys(ActualObject).length) { return false } for (const key in ExpectedObject) { // if ActualObject does not have a property that is // on ExpectedObject, it is not equal if (!(key in ActualObject)) { return false } // if a property's value on ActualObject is not equal // with a strict comparison, to the equivalent property // on ExpectedObject, it is not equal if (ActualObject[key] !== ExpectedObject[key]) { return false } } return true } console.log(isEqual({ a:1 }, { a: 1 })) //=> true console.log(isEqual({ a:1 }, { a: "1" })) //=> false console.log(isEqual({ a:1 }, { a: 1, b: 2 })) //=> false接收消息。然后,它使用.transform(this::myFirstCustomTransformer)方法调用您的自定义代码,并将调用结果发送到inputChannel的{​​{1}}。

因此,您在流程中需要的是myFirstCustomTransformer之前和之后的outputChannel。然后,在单元测试中,您向MessageTransformingHandler发送测试消息,并在.channel()中验证结果。

我同意我们需要提供一种简单易用的解决方案,但是首先让我们从您的用例开始吧!