如何模仿Camel Endpoint POJO制作人

时间:2015-12-10 09:29:53

标签: java apache-camel

我跟着指南here,我成功地在我的bean端点上配置了一个生产者,如下所示:

@Produce( uri = "activemq:foo" )
  private MyListener myListener; 

MyListener是:

public interface MyListener {

  @InOnly
  public void send( String message );
}

和我的豆子:

    public class MyBeanEndpoint {

          @Produce( uri = "activemq:foo" )
          private MyListener myListener; 

          @Handler
          public void doSomething( final Object body ) {
            ...
    }

public void setMyListener( final MyListener myListener ) {
    this.myListener = myListener;
  }

现在,我该怎么测试呢? 我的意思是:我的测试延伸CamelTestSupport,我用

配置了我的路线
@Override
  public RouteBuilder createRouteBuilder() {
    return new RouteBuilder() { ... }

那就是:我已经复制了驼峰语境,但是我没有配置弹簧上下文,我希望(如果可能的话)避免实例化它。

我如何模拟生产者或使Camel实例化并将此bean注入我的bean端点? 使用像CamelTestSupport和类似实用程序这样的Apache Camel功能来测试这种情况的最佳方法是什么?

我的参考测试是:

public class Test extends CamelTestSupport {

  private static BeanEndpoint beanEndpoint

  @BeforeClass
  public static void init() { 
    beanEndpoint.setActivemqMyListener( ??? );
  }

  @Override
  public CamelContext createCamelContext() {
    context = new DefaultCamelContext();
    context.addComponent( "activemq", new SedaComponent() );
    return context;
  }

  @Override
  public RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

      @Override
      public void configure() throws Exception {

        from( "activemq:foo" )
                .to( "mock:out" );

      }
    };
  }

  @Test
  public void testFooQueue() throws Exception {}

1 个答案:

答案 0 :(得分:1)

让Camel创建你的bean,然后为你配置各种依赖注入和诸如此类的东西。

private static BeanEndpoint beanEndpoint

 ...

beanEndpoint = camelContext.getInjector().newInstance(BeanEndpoint.class);