0
votes

I have the following in a unit test for a Camel route:

RouteUnitTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {RouteUnitTestConfig.class})
public class RouteUnitTest extends CamelTestSupport {
    @Autowired
    MQProperties mqProps;
    @Autowired
    Route route;

    @Produce(uri="...")
    ProducerTempalte template;

    @Test
    //rest of test class

}

The MQProperties object pulls properties from property files, and for unit tests it reads the application-unit-test.properties file, which uses SEDA endpoints for unit testing. I can't seem to get the expression correct in the @Produce tag; I want to set the ProducerTemplates uri to mqProps.getReceiveQueue(), but out of the formats I've tried, I get an exception regarding setting a default endpoint for the producer. I know that it's not an issue with the value that should be passed in, calling template.send(mqProps.getReceiveQueue(), exchange); for example works just fine. I have tried the following formats:

@Produce(uri="#{@mqProps.getReceiveQueue}")
@Produce(uri="{{mqProps.getReceiveQueue}}")
@Produce(uri="#{mqProps.getReceiveQueue}")

These have thrown the exception mentioned. Am I getting the EL format wrong, or is it an issue with the autowiring?i.e. Maybe the object isn't populated by the time the producer uri stuff is all hooked up? I can't find much else on this, Camel resources talk more around Bean injction etc, but any resources I've seen thus far on the @Produce annotation usually use a hardcoded string, or read from a property file directly

1

1 Answers

0
votes

I believe the problem is with the BeanExpressionContext

Spring needs this to evaluate SPEL. Here is a simpleexample to solve this issue. Adapt it to your project.

  public class TestingSupport {

  @Bean
  public BeanExpressionContext setBeanExpressionContext(@Autowired MQProperties mqProps, @Autowired ApplicationContext applicationContext){
    return new BeanExpressionContext((ConfigurableBeanFactory) applicationContext.getAutowireCapableBeanFactory(), null);
  }
}    

And the testing class would look like this.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {RouteUnitTestConfig.class, MQProperties.class, TestingSupport.class})
@TestPropertySource("classpath:application-test.properties")
public class RouteUnitTest extends CamelTestSupport {

  @Value("#{MQProperties['receiveQueue']}")
  String recieveQueue;

  @Test
  public void test(){
      assertEquals(1, 1);
  }

}