【发布时间】:2015-11-23 16:01:05
【问题描述】:
我正在集成测试一个 spring 集成流,它从一个邮件入站通道适配器开始。我将测试电子邮件发送到模拟 GreenMail 电子邮件服务器,然后测试预期结果。但是因为邮件是异步的,所以目前只有在我发送邮件后等待,直到流程完成,测试才能通过。
这是邮件适配器配置:
<int-mail:inbound-channel-adapter id="imapAdapter"
store-uri="#{mailConnectionString}"
java-mail-properties="javaMailProperties" channel="inboundChannel"
should-delete-messages="false" should-mark-messages-as-read="true"
auto-startup="true">
<int:poller id="emailPoller" max-messages-per-poll="1" fixed-rate="5000">
</int:poller>
</int-mail:inbound-channel-adapter>
所以,参考这个:Adding Completion Advice,我想我可以简单地等待完成建议,然后继续测试。但是您不能向邮件适配器添加建议:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'adviceChain' of bean class [org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean]: Bean property 'adviceChain' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
还尝试了回复生成处理程序(来自上面的链接),但没有找到 bean。
所以。如何向入站邮件适配器添加建议?或者有没有更好的方法在整个流程完成后测试邮件适配器?
回答建议后更新 我更改了测试以在设置中添加建议。
@Autowired
private SourcePollingChannelAdapter emailAdapter;
private MyAdvice imapAdapterCompletionAdvice;
@Before
public void setup() throws Exception
{
imapAdapterCompletionAdvice = new MyAdvice();
List<Advice> theAdvice = new ArrayList<Advice>();
theAdvice.add(imapAdapterCompletionAdvice);
emailAdapter.setAdviceChain(theAdvice);
emailAdapter.start();
}
但是没有调用建议。我错过了什么吗?
这是 Advice 类:
public class MyAdvice implements MethodInterceptor {
private final CountDownLatch latch = new CountDownLatch(1);
public Object invoke(MethodInvocation invocation) throws Throwable {
Object proceed = invocation.proceed();
System.out.println(proceed);
if (proceed instanceof Boolean) {
Boolean mailReceived = (Boolean) proceed;
if(mailReceived){
latch.countDown();
}
}
return proceed;
}
public CountDownLatch getLatch() {
return latch;
}
}
【问题讨论】: