1

in my spring-boot project i have the following class :

@Configuration
@EnableIntegration
public class ConsumeFiles {

    @Bean
    public IntegrationFlow inboundFileFromDirectory(@Value("${period}") long period,
                                                          @Value("${poller.max.messages.per.poll}") int maxMessagesPerPoll,
                                                          MessageSource<File> fileReadingFromSrc) {
        return IntegrationFlow.from(fileReadingFromSrc,
                        c -> c.poller(Pollers.fixedDelay(period)
                        ))
                .transform(Files.toStringTransformer())
                .channel("channel-1")
                .get();
    }
    
    @ServiceActivator(inputChannel = "channel-1")
    public void controleSignature(final Message<File> file) throws IOException {
        // do things 
    }
    
        @Bean
    public MessageSource<File> fileReadingFromSrc() {
        FileReadingMessageSource source = new FileReadingMessageSource();
        source.setDirectory(new File(CONST_PATH));
        source.setAutoCreateDirectory(true);
        return source;
    }
    
}

pretty basic : it monitors a directory, and when a file is dropped in it the flow starts (eg: control signature file and do other things) i would like to unit test a such class. i have already read a lot of resources on spring pages but i am really bad with testing. can someone please advice in a really simple way ?

1 Answer 1

0

Please, take a look into Testing support in Spring Integration: https://docs.spring.io/spring-integration/reference/testing.html.

You probably would need to look into MockIntegration.mockMessageSource() to replace your FileReadingMessageSource with something what would give you some testing data file instead of trying to attach into a source directory.

Another feature is to use MockIntegrationContext.substituteTriggerFor() which can be based on the OnlyOnceTrigger instead of Pollers.fixedDelay(period).

In the you can use MockIntegrationContext.substituteMessageHandlerFor() to replace your real controleSignature() with a MockIntegration.mockMessageHandler().

If your idea is just to test controleSignature() method, then you can call it directly or send a message into its channel-1 input.

Not the answer you're looking for? Browse other questions tagged or ask your own question.