javaspring-bootapache-camelcxfcamel-test

Camel Spring Boot CXF endpoint testing


I have following endpoints and route.

  @Bean
  public CxfEndpoint requestEndpoint() {
    CxfEndpoint endpoint = new CxfEndpoint();
    endpoint.setAddress(SERVICE_ADDRESS);
    endpoint.setServiceClass(Service.class);
    endpoint.setWsdlURL(WSDL_LOCATION);
    endpoint.setBus(bus);
    endpoint.setProperties(endpointProperties);
    return endpoint;
  }

And

from("cxf:bean:requestEndpoint")
  //Custom logic with various outbound routes 
  .choice()
  ....

  .to("direct:route1")

  ....

  .to("direct:route2") 

I want to test it. Various input data should be routed to various route.

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@MockEndpoints
@Configuration
public class RequestRouteTest extends CamelTestSupport {

  @Autowired
  private ProducerTemplate producerTemplate;


  @EndpointInject(uri = "mock:direct:route1")
  private MockEndpoint mockCamel;


  @Test
  public void myTest() throws Exception {
    mockCamel.expectedMessageCount(1);

    producerTemplate.sendBody("cxf:bean:requestEndpoint", bodyForRoute1);

    mockCamel.assertIsSatisfied();
  }

} 

But in this case i have following error:

Caused by: java.net.ConnectException: ConnectException invoking http://myurl: Connection refused (Connection refused)

this is logical, I did not run the application.

Then i try to replace cxf endpoint to mock:

MockEndpoint mockEndpoint = getMockEndpoint("mock:cxf:bean:requestEndpoint");
producerTemplate.sendBody(mockEndpoint, bodyForRoute1);

And i got

Asserting: mock://direct:route1 is satisfied - FAILED

and exception (java.lang.AssertionError: mock://direct:route1 Received message count. Expected: <1> but was: <0> ), because my route code was not invoked.

How to properly test the route? I would like to try two interesting ways:

1) Test with real http endpoint (this allows you to test the early phases of request - for example - requests with invalid xml)

2) Isolated test when the POJO payload is in the message body.

I would be grateful if there was a solution to my problem


Solution

  • The route test in your question uses the Camel test kit. This is a great tool to do "unit tests" for your Camel routes, i.e. #2 of your questions.

    In these tests you typically use AdviceWith to replace real endpoints with mocks because you want to test the correct routing of messages.

    See the linked answer in @Bedlas comment to replace your CXF endpoint with a direct endpoint to make your test work.

    If you want to do tests with real endpoints, i.e. #1 of your questions, you should consider using an integration test framework like Citrus.

    With such frameworks you write tests against a running instance of your application. In your case you would send an HTTP or SOAP request against the real CXF endpoint of your running application and you have loads of possibilities to verify the outcome (check JMS queues, database entries etc) depending on what your application does.