Home > Blockchain >  How can we mock test IntegrationFlow in spring boot. I am using Spring Integration inside a Spring b
How can we mock test IntegrationFlow in spring boot. I am using Spring Integration inside a Spring b

Time:05-13

I have a task where I need to implement IntegrationFlow in Spring boot app and for that I created one project. Below are 3 classes.

IntegrationConfig.java

@Configuration
public class IntegrationConfig {

   @Autowired
   private Transformer transformer;

   @Bean
   public IntegrationFlow integrationFlow() {

       return IntegrationFlows.from(fileReader(),spec -> spec.poller(Pollers.fixedDelay(500)))
       .transform(transformer,"transform")
       .handle(fileWriter())
       .get();

}



@Bean
   public FileWritingMessageHandler fileWriter() {

   FileWritingMessageHandler handler = new FileWritingMessageHandler(new File("destination"));
   handler.setExpectReply(false);
   return handler;
}



@Bean
   public FileReadingMessageSource fileReader() {

   FileReadingMessageSource source = new FileReadingMessageSource();
   source.setDirectory(new File("source"));
   return source;
}

}

Transformer.java

@Component
   public class Transformer {

   public String transform(String filePath) throws IOException {

   String content = new String (Files.readAllBytes(Paths.get(filePath)));
   return "Transformed: "   content;
}

}

SpringIntegrationExample.java

@SpringBootApplication
   public class SpringIntegrationExample {

   public static void main(String[] args){
   SpringApplication.run(SpringIntegrationExample.class,args);
}
}

Now there is task to mock tests this.

I am new to test and not sure how to test this using either JUnit or Mockito or both. I tried with JUnit with When().thenReturn() but not able to test IntegrationFlow in that.

Can anyone help me on this ? Thanks in advance.

CodePudding user response:

A couple notes for your configuration so far:

The FileReadingMessageSource produces File as payload for messages. The FileWritingMessageHandler can deal with File payloads. Therefore you don't need that transform() in between.

Although you just show not the whole flow...

There is Java DSL-specific factory for fluent API: org.springframework.integration.file.dsl.Files.

It is not clear what you'd like to mock, but here is a dedicated documentation about testing with Spring Integration: https://docs.spring.io/spring-integration/docs/current/reference/html/testing.html#testing

  • Related