title: In an Apache Camel application, how can unit tests inject mock endpoints in place of real ones?
body: <p>I am implementing a <a href="http://camel.apache.org/message-translator.html" rel="noreferrer">message translator pattern</a> with Apache Camel, to consume messages from a RESTful endpoint and send them onward to an AMQP endpoint.  </p>
  
  <p>The enclosing application is based on Spring Boot, and so I'm using Camel's "<a href="http://camel.apache.org/spring-boot.html" rel="noreferrer">spring-boot</a>" component to integrate the two frameworks.  As suggested by the documentation in this spring-boot link, I'm implementing my Camel route inside of a <code>@Configuration</code>-annotated class which extends <code>RouteBuilder</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Component
  public class MyRestToAmqpRouter extends RouteBuilder {
  
     @Override
     public void configure() throws Exception {
  
        from("jetty:http://my-restful-url")
           .process(exchange -&gt; {
              // convert the message body from JSON to XML, take some 
              // incoming header values and put them in the outgoing
              // body, etc...
           }).to("rabbitmq://my-rabbitmq-url");
  
     }
  
  }
  </code></pre>
  
  <p>My question involves how to go about unit-testing this translation, without needing an actual RESTful endpoint or configured RabbitMQ broker?  I've read many online examples, as well as the <em>Camel in Action</em> book... and it seems like the typical approach for unit testing a Camel route is to cut-n-paste the route into your unit test, and replace one or more endpoint URL's with "<code>mock:whatever</code>".</p>
  
  <p>I guess that <em>sorta</em> works... but it's awfully brittle, and your test suite won't recognize when someone later changes the real code without updating the unit test.</p>
  
  <p>I've tried to adapt some Spring-based unit testing examples with mocks, like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(CamelSpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = {Application.class})
  public class MyRestToAmqpRouterTest extends AbstractJUnit4SpringContextTests {
  
      @Produce(uri = "jetty:http://my-restful-url")
      private ProducerTemplate fakeRest;
  
      @EndpointInject(uri = "rabbitmq://my-rabbit-url")
      private MockEndpoint fakeRabbit;
  
      @Test
      @DirtiesContext
      public void testRouter() throws InterruptedException {
          fakeRabbit.expectedMessageCount(1);
          fakeRest.sendBodyAndHeader("", "header-1", "some value");
          fakeRabbit.assertIsSatisfied();
      }
  
  }
  </code></pre>
  
  <p>My hope was that Camel would take those endpoint URLs from the unit test, register them as mocks... and then use the mocks rather than the real endpoint when the real code tries to use those URLs.  </p>
  
  <p>However, I'm not sure that this is possible.  When I use the real URLs in the unit test I get <code>IllegalArgumentException</code>'s, because you apparently can't inject a "real" endpoint URL into a <code>MockEndpoint</code> instance (only URLs prefixed with "<code>mock:</code>").  </p>
  
  <p>When I do use a "<code>mock:...</code>" endpoint URL in my unit test, then it's useless because there's nothing tying it to the real endpoint URL in the class under test.  So that real endpoint URL is never overridden.  When the real code is executed, it just uses the real endpoint as normal (and the goal is to be able to test without an external dependency on RabbitMQ).</p>
  
  <p>Am I missing something on a really fundamental level here?  It seems like there would be a way for unit tests to inject fake routes into a class like this, so that the code under test could switch from real endpoints to mock ones without even realizing it.  Alternatively, I suppose that I could refactor my code so that the anonymous <code>Processor</code> were elevated to a standalone class... and then I could unit test its translation logic independently of the route.  But that just seems like an incomplete test.</p>