Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-8064

MockEndpointsAndSkip annotation does not resolve property placeholders

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 2.13.2
    • 2.13.4, 2.14.1, 2.15.0
    • None
    • None
    • Unknown

    Description

      We are using property placeholders in our route configuration in order to switch components in our unit tests. For example:

              from(MyRouteBuilder.ENTRY_ENDPOINT)
                  .routeId("some.route")
                  .process(doSomething())
                  .to(MyRouteBuilder.EXIT_ENDPOINT);
      

      Where ENTRY_ENDPOINT = "entry.endpoint.uri"
      and EXIT_ENDPOINT = "exit.endpoint.uri"

      Then, in production, the Camel context is initialized with a properties file where the URIs use the "jms" component, while in our unit tests we initialise the context with a 'test' properties file where the URIs use the "direct" component. For example:

      # test.properties
      entry.endpoint.uri = direct:myRouteBuilder.entry
      exit.endpoint.uri = direct:myRouteBuilder.exit
      

      A typical RouteBuilder unit test looks something like this:

      @RunWith(CamelSpringJUnit4ClassRunner.class)
      @ContextConfiguration(
              classes = { MyRouteBuilderTest.TestConfig.class },
              loader = CamelSpringDelegatingTestContextLoader.class
      )
      @MockEndpointsAndSkip(value =  "direct:myRouteBuilder.exit")
      public class MyRouteBuilderTest {
      
          @Produce(uri = MyRouteBuilder.ENTRY_ENDPOINT)
          private ProducerTemplate myRouteBuilderProducer;
      
          @EndpointInject(uri = "mock:" + MyRouteBuilder.EXIT_ENDPOINT)
          private MockEndpoint mockOutputServiceEndpoint;
      
          @Autowired
          private CamelContext camelContext;
      
          @Test
          public void testSomething() {
              ....
          }
      
          @Configuration
          public static class TestConfig extends SingleRouteCamelConfiguration {
              @Override
              protected void setupCamelContext(CamelContext camelContext) throws Exception {
                  super.setupCamelContext(camelContext);
                  PropertiesComponent prop = camelContext.getComponent("properties", PropertiesComponent.class);
                  prop.setLocation("myRouteBuilder.test.properties");
              }
      
              @Override
              public RouteBuilder route() {
                  return new MyRouteBuilder();
              }
          }
      

      The issue we're having is that the @MockEndpointsAndSkip annotation on the test class does not resolve property placeholders, therefore we have to write the resolved value instead of the property placeholder value (which is a public static variable in the RouteBuilder implementation) I.e. we want to use MyRouteBuilder.EXIT_ENDPOINT instead of "direct:myRouteBuilder.exit".

      It would be nicer to avoid having duplicate extra hard-coded Strings if possible, so that if the endpoint uri value is updated in the property file then all the tests don't also have to be updated.

      Attachments

        Activity

          People

            njiang Willem Jiang
            westerg Gareth Western
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: