Search code examples
javaapache-camelcamel-test

Camel testing - java.lang.IllegalArgumentException: defaultEndpoint must be specified


I am trying to create testcases for my camel route using http://camel.apache.org/mock.html . I need to verify the the processors in the route. But the simple test is not working for me.

public class CamelRouteTest  extends CamelTestSupport {

  @Override
  public String isMockEndpointsAndSkip() {
    // override this method and return the pattern for which endpoints to mock,
    // and skip sending to the original endpoint.
    return "mock:result";
  }

  @Test
  public void verifyMessageCount() throws Exception {
    template.sendBody("Test");
    getMockEndpoint("mock:result").expectedMessageCount(1);
    assertMockEndpointsSatisfied();
  }

  @Override
  protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("mock:result");
        }
    };
  }
}

Stacktrace:

java.lang.IllegalArgumentException: defaultEndpoint must be specified
    at org.apache.camel.util.ObjectHelper.notNull(ObjectHelper.java:308)
    at org.apache.camel.impl.DefaultProducerTemplate.getMandatoryDefaultEndpoint(DefaultProducerTemplate.java:506)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:370)

Solution

  • The template.sendBody("Test") try to send Test to the default endpoint. As in your code this is not configured it fails.

    You could:

    • specify which endpoint to use

      template.sendBody("direct:start", "Test");
      
    • get an endpoint from the context and set it as the default endpoint

      Endpoint endpoint = context.getEndpoint("direct:start");
      template.setDefaultEndpoint(endpoint);
      template.sendBody("Test");