javascript
Spring集成文件轮询和测试
我最近實施了一個小項目,在該項目中,我們必須輪詢文件夾中的新文件,然后在文件內容上觸發服務流。
Spring Integration非常適合此要求,因為它帶有一個通道適配器 ,該適配器可以掃描文件夾中的新文件,然后通過消息傳遞流程獲取文件。
本文的目的是研究如何測試文件輪詢器流。
首先考慮使用以下Spring集成配置實現的簡單流程:
在圖中更好地表示了這一點:
流程簡單測試
現在要測試此流程,我的第一種方法是將示例文件放入類路徑中的文件夾中,在測試過程中動態發現并使用此文件夾名稱,并將最終消息寫入測試通道,并對進入該通道的消息進行斷言。 這是組成原始Spring配置的測試配置的樣子:
<beans:beans xmlns="http://www.springframework.org/schema/integration"....<beans:import resource="si-fileprocessing.xml"/><util:properties id="localProps"><beans:prop key="inbound.folder">#{new java.io.File(T(java.io.File).getResource("/samplefolder/samplefile.txt").toURI()).parent}</beans:prop></util:properties><channel id="testChannel"><queue/></channel><bridge id="loggingChannelAdapter" input-channel="processedFileChannel" output-channel="testChannel"></bridge> </beans:beans>測試看起來像這樣:
@Autowired @Qualifier("testChannel") PollableChannel testChannel;@Test public void testService() throws Exception{Message<?> message = this.testChannel.receive(5000);assertThat((String)message.getPayload(), equalTo("Test Sample file content")); }這可以很好地工作,尤其要注意,可以使用Spring-EL表達式在配置中完全提供路徑:
#{new java.io.File(T(java.io.File).getResource("/samplefolder/samplefile.txt").toURI()).parent}模擬服務
現在,進一步介紹一下,如果我想模擬正在處理文件的服務(files.RealFileHandlingService),該怎么辦。 一旦實現了模擬,這里就會出現一個問題。 注入模擬服務的一種方法是使用Mockito和它提供的幫助程序功能,以使用Spring配置文件通過這種方式創建模擬:
<beans:bean name="fileHandlerService" factory-method="mock" class="org.mockito.Mockito"><beans:constructor-arg value="files.FileHandlingService"></beans:constructor-arg> </beans:bean>創建此模擬bean之后,可以通過以下方式在junit的@Before注釋方法中添加模擬行為:
@Before public void setUp() {when(fileHandlingServiceMock.handle((File)(anyObject()))).thenReturn("Completed File Processing"); }@Test public void testService() throws Exception{Message<?> message = this.testChannel.receive(5000);assertThat((String)message.getPayload(), equalTo("Completed File Processing")); }如果現在重復測試,令人驚訝的是它將失敗,原因是–在調用Junit的@Before方法時,Spring應用程序上下文已完全初始化,并且輪詢進入文件夾的文件的輪詢器在模擬之前被觸發行為被添加,因此,如果沒有正確的模擬服務行為,測試將失敗。
我確定還有其他修復程序,但是對我有用的修復程序實際上是控制掃描哪個文件夾以及在什么時候將文件放置在文件夾中以觸發流。 這很大程度上基于我在Spring Integration項目本身中看到的一些測試 。 訣竅是先使用Spring config創建一個臨時文件夾,然后將該文件夾設置為輪詢文件夾:
<beans:bean id="temp" class="org.junit.rules.TemporaryFolder"init-method="create" destroy-method="delete"/><beans:bean id="inputDirectory" class="java.io.File"><beans:constructor-arg value="#{temp.newFolder('sitest').path}"/> </beans:bean> <util:properties id="localProps"><beans:prop key="inbound.folder">#{inputDirectory.getAbsolutePath()}</beans:prop> </util:properties>現在,僅在測試運行期間將文件放置在臨時文件夾中,這樣@Before注釋方法就有機會向模擬添加行為,并且可以明確斷言模擬的行為:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("si-test.xml") public class FileHandlingFlowTest {@Autowired @Qualifier("testChannel") private PollableChannel testChannel;@Autowired private FileHandlingService fileHandlingServiceMock;@Autowired @Qualifier("inputDirectory") private File inputDirectory;@Beforepublic void setUp() {when(fileHandlingServiceMock.handle((File)(anyObject()))).thenReturn("Completed File Processing");}@Testpublic void testService() throws Exception{Files.copy(new File(this.getClass().getResource("/samplefolder/samplefile.txt").toURI()), new File(inputDirectory, "samplefile.txt"));Message<?> message = this.testChannel.receive(5000);assertThat((String)message.getPayload(), equalTo("Completed File Processing"));} } 參考: all和其他博客中來自JCG合作伙伴 Biju Kunjummen的Spring集成文件輪詢和測試 。翻譯自: https://www.javacodegeeks.com/2013/05/spring-integration-file-polling-and-tests.html
總結
以上是生活随笔為你收集整理的Spring集成文件轮询和测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华硕台式电脑怎么重装系统(华硕台式机重装
- 下一篇: 非居民是什么意思 什么是非居民