mardi 26 janvier 2016

Testing Spring Integration not setting up IntegrationFlows

I am having trouble with testing my application which is using Spring Integration and Spring Integration DSL. When I run my application the flows are set up correctly with no issue however for my tests I want to isolate certain flows and carry out some component tests on them so I need to have the flows in isolation i.e. can't point at the SpringBootApplication class otherwise the other flows within the application are picked up. I have experimented with both the @SpringApplicationConfigurationand @ContextConfiguration annotations with no success.

I have set up my test class (See the code below):

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MyService.class, MyServiceTestBeans.class})
@TestPropertySource("classpath:/test_application.properties")
public class MyServiceTest {
    @Autowired
    private RestOperations restOperations;

    @Autowired
    private MyService testObject;

    @Test
    public void testRequestValidObject() throws IOException {
        MyObject expectedMyObject = new MyObject();

        final String id = "valid-id";
        expectedMyObject.setId(id);
        when(restOperations.exchange(any(RequestEntity.class), eq(MyObject.class)))
            .thenReturn(ResponseEntity.ok(expectedMyObject));
        final Message<String> messageToSend = MessageBuilder.withPayload(id)
                .build();
       testObject.inputChannel().send(messageToSend);

        ((DirectChannel) testObject.outputChannel()).subscribe(message -> {
            assertEquals(expectedMyObject, message.getHeaders().get("myObject", MyObject.class));
        });

    }
}

My MyService class:

@Configuration
public class MyService {
    @Autowired
    private RestOperations restOperations;

    @Value("myServiceWebService")
    private String webServiceName;

    @Bean
    public MessageHandler webServiceMessageHandler() {
         return new WebServiceMessageHandler(webServiceName, restOperations);
    }

    @Bean
    public MessageChannel inputChannel() {
        return MessageChannels.direct("inputChannel").get();
    }

    @Bean
    public MessageChannel outputChannel() {
       return MessageChannels.direct("outputChannel").get();
    }

    @Bean
    public MessageChannel myObjectChannel() {
        return Message.direct("myObject").get();
    }

    @Bean
    public IntegrationFlow getMyObject() {
          return IntegrationFlows.from(inputChannel())
               .handle(webServiceMessageHandler())
               .get();
    }

    @Bean
    public IntegrationFlow returnMyObjectToOutputFlow() {
        return IntegrationFlows.from(myObjectChannel())
             .channel(outputChannel())
             .get();
    }
}

Unlike when the application is run there is no output regarding Spring wiring together the flow however there is output on the console stating it has picked up the IntegrationFlow beans but it hasn't done anything with them. Thus leaving me with a hideous 'dispatcher has no subscribers' Messaging exception when the test is run (caused by testObject.inputChannel().send(messageToSend)).

It appears that the way the application starts is completely different and the step which sets up the IntegrationFlows are skipped when the tests are run.

Is there something I am missing? Is there a way of setting up the configuration for the tests to be solely the beans needed by this one component?

Any help is gratefully received!

Aucun commentaire:

Enregistrer un commentaire