Reputation: 15
I am trying to test
@Bean
public IntegrationFlow integrationFlow(JobLaunchingGateway jobLaunchingGateway) {
return IntegrationFlows.from(inventoryImportInboundChannelAdapter,
p -> p.poller(Pollers.fixedRate(inventoryImportJobProperties.getPollingFrequency()))).
handle(fileMessageToPath()).
handle(fileMessageToJobRequest()).
handle(jobLaunchingGateway).
log(LoggingHandler.Level.INFO, "headers.id + ': ' + payload").
get();
}
Inventory import channel adapter is s3 adapter, i dont want to connect to S3 for component test. I tried using MockIntegrationContext and it did not work . Please advise
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ImportInventoryJobIntegrationFlow.class})
@SpringIntegrationTest
public class ImportInventoryJobIntegrationFlowTest {
@MockBean
private MessageSource<?> inventoryImportInboundChannelAdapter;
@MockBean
private Job inventoryImportJob;
@MockBean
private JobRepository jobrepository;
@MockBean
private InventoryImportJobProperties inventoryImportJobProperties;
@Autowired
private MockIntegrationContext mockIntegrationContext;
@Test
public void testChannelAdapter(){
File importFile = Mockito.mock(File.class);
BDDMockito.given(importFile.getParent()).willReturn("test.import");
System.out.println(mockIntegrationContext);
this.mockIntegrationContext.substituteMessageSourceFor("inventoryImportInboundChannelAdapter",
MockIntegration.mockMessageSource(importFile));
}
}
Error getting is: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'inventoryImportInboundChannelAdapter' available
Upvotes: 1
Views: 981
Reputation: 121177
Please, refer to the mockIntegrationContext.substituteMessageSourceFor()
JavaDocs:
/**
* Replace the real {@link MessageSource} in the {@link SourcePollingChannelAdapter} bean
* with provided {@link MessageSource} instance.
* Can be a mock object.
* @param pollingAdapterId the endpoint bean name
* @param mockMessageSource the {@link MessageSource} to replace in the endpoint bean
* @see org.springframework.integration.test.mock.MockIntegration#mockMessageSource
*/
public void substituteMessageSourceFor(String pollingAdapterId, MessageSource<?> mockMessageSource) {
The key word there is SourcePollingChannelAdapter
. This bean is a result of your
IntegrationFlows.from(inventoryImportInboundChannelAdapter,
p -> p.poller(Pollers.fixedRate(inventoryImportJobProperties.getPollingFrequency())))
Unfortunately you don't specify here that inventoryImportInboundChannelAdapter
, so its target name is generated.
Consider to add .id("inventoryImportInboundChannelAdapter")
before or after poller()
definition for that endpoint.
UPDATE
We have a test configuration like this:
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlows
.from(() -> new GenericMessage<>("myData"),
e -> e.id("mySourceEndpoint"))
.<String, String>transform(String::toUpperCase)
.channel(results())
.get();
}
Pay attention to the e.id("mySourceEndpoint")
.
And then in the test we do like this:
this.mockIntegrationContext.substituteMessageSourceFor("mySourceEndpoint",
MockIntegration.mockMessageSource("foo", "bar", "baz"));
Upvotes: 1