Search code examples
javajunitmockitospring-integration-dsl

Junit test cases for Integration flow and generic handler


I want to write a JUnit test case and integration test case for Integration flow and GenericHandler.

I have gone through some articles but didn`t found anything useful.

Code Snippet 1

IntegrationFlows.from(() -> path, e -> e.poller(Pollers.fixedDelay(60, TimeUnit.SECONDS)))
.handle(Sftp.outboundGateway(sftpSessionFactory(), LS, "payload")
        .regexFileNameFilter(".*csv"))
.split()
.handle(Sftp.outboundGateway(sftpSessionFactory(), GET, "payload.remoteDirectory + payload.filename").options(STREAM).temporaryFileSuffix("_reading"))
.handle(readCsvData(), e -> e.advice(afterReadingCsv()))
.filter(this, "checkSuccess")
.enrichHeaders(h -> h
        .headerExpression(FileHeaders.RENAME_TO, "headers[file_remoteDirectory] + 'archive/' + headers[file_remoteFile]")
        .headerExpression(FileHeaders.REMOTE_FILE, "headers[file_remoteFile]")
        .header(FileHeaders.REMOTE_DIRECTORY, "headers[file_remoteDirectory]"))
.handle(Sftp.outboundGateway(sftpSessionFactory(), MV, "headers[file_remoteDirectory]+headers[file_remoteFile]").renameExpression("headers['file_renameTo']"))
.get();

Code Snippet 2

    public GenericHandler readCsvData() {
        return new GenericHandler() {
            @Override
            public Object handle(Object o, Map map) {
        }
       }
       }

It will be useful to get some direction for writing JUnit test cases for the above code snippets.


Solution

  • You can do something like below

    Solution code snippet 1: Mock the class and call the method then you can verify the interactions.

    Solution code snippet 2 :

    File file = ResourceUtils.getFile("classpath:someFile");
    InputStream inputStream = new FileInputStream(file);
    Map map = anyMap();
    GenericHandler genericHandler = service.readCsvData();        
    Object actual = genericHandler.handle(inputStream, map);
    // Add some assertions here