2017-04-26 56 views
1

如何使Java ServiceActivator在JUnit Test中可見?如何使Java ServiceActivator在JUnit Test中可見?

我已經開始編寫一個測試,它通過一些Java Config文件(即在@ContextConfiguration中設置的文件)導入一些spring集成xml。其中一個xml文件引用了一個名爲的通道pollerErrorChannel這是在Java類中聲明的ServiceActivator的輸入通道。當測試澎湃,我得到以下錯誤:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sftpInboundAdapterBusiness': Cannot create inner bean '(inner bean)#1fe8d51b' of type [org.springframework.integration.scheduling.PollerMetadata] while setting bean property 'pollerMetadata'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1fe8d51b': Cannot create inner bean '(inner bean)#324dcd31' of type [org.springframework.integration.channel.MessagePublishingErrorHandler] while setting bean property 'errorHandler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#324dcd31': Cannot resolve reference to bean 'pollerErrorChannel' while setting bean property 'defaultErrorChannel'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'pollerErrorChannel' available

下面在我的測試

@RunWith(SpringRunner.class) 
@ContextConfiguration(classes = {PropertySourcesPlaceholderConfigurer.class, 
    SFTPSampleReceiver.class, 
    SampleIngestBusinessConfig.class, 
    SampleIngestConfig.class, 
    SessionFactoryConfig.class}, 
    initializers = ConfigFileApplicationContextInitializer.class) 
@TestPropertySource(locations={"/application.yml"}) 
public class BusinessSampleRecieverTests { 


    @Test 
    public void test() { 

    } 

}

段從樣品採集,business.xml指定pollerErrorChannel作爲頻道

<int-sftp:inbound-channel-adapter id="sftpInboundAdapterBusiness" 
     channel="sftpInboundBusiness" 
     session-factory="sftpSessionFactory" 
     local-directory="${sftp.localdirectory}/business-local" 
     filter="businessCompositeFilter" 
     remote-file-separator="/" 
     remote-directory="${sftp.directory}/business-sftp"> 
    <int:poller cron="${sftp.cron}" max-messages-per-poll="1" error-channel="pollerErrorChannel"/> 
</int-sftp:inbound-channel-adapter> 

這裏是Java類指定pollerErrorChannel作爲InputChannel到@ServiceActivator

@Slf4j 
@MessageEndpoint 
@Component 
public class SFTPSampleReceiver { 

    @ServiceActivator(inputChannel = "pollerErrorChannel", outputChannel = "errorUploadChannel") 

    public Message<String> processInvalidSample(GenericMessage errorMessage) { 

     String error = ((Exception) errorMessage.getPayload()).getCause().toString(); 
     String fileName = ((MessagingException) errorMessage.getPayload()).getFailedMessage().getHeaders() 
      .get("file_name").toString(); 
     String directory = ((MessagingException) errorMessage.getPayload()).getFailedMessage().getHeaders() 
      .get("sample_type").toString() + "-sftp"; 
     String shortFileName = fileName.replace(".xml", ""); 
     String errorFile = shortFileName + "_error.txt"; 

     log.debug(fileName + " Was invalid and rejected."); 

     final Message<String> message = MessageBuilder.withPayload(error).setHeader("error_file_name", 
      errorFile).setHeader("file_name", fileName).setHeader("short_file_name", 
      shortFileName).setHeader("directory", directory).build(); 

     return message; 
    } 


} 

感謝

+1

您正在使用什麼版本的Spring集成的?嘗試將'@ EnableIntegration'添加到您的一個'@ Configuration'類或''到您的XML中。 –

+0

起作用了,謝謝。我使用4.3.7版 – sdiaz1000

回答

0

Garys有關添加< INT評論:註解配置/>您的XML工作

0

您必須聲明,pollerErrorChannel豆。

僅僅有一個@ServiceActivator(inputChannel = "pollerErrorChannel"它已經晚了才能自動創建該頻道。 <poller>被解析並且稍早被填充爲一個bean。

我們可能會檢查PollerParser使用MessagePublishingErrorHandler.setDefaultErrorChannelName()而不是errorHandler.addPropertyReference("defaultErrorChannel", errorChannel);來解決頻道晚點播。

隨意就此事籌集JIRA