如何在使用bean的camel单元测试中注册bean?

der*_*itz 3 java junit guice apache-camel jukito

我想对在java中使用bean配置的单个路由进行单元测试.我在骆驼的行动中读到(第6.1.4章)如何做到这一点:

protected RouteBuilder createRouteBuilder() throws Exception {
    return new myRoute();
}
Run Code Online (Sandbox Code Playgroud)

但在我的情况下,溃败需要注册一些bean.我知道如何在独立应用程序中注册bean:请参阅此处 但是如何在"CamelTestSupport"中注册bean?有没有办法在没有注册表的情况下使用bean?可能通过注入它们(所有bean都没有arg构造函数)?我正在使用Guice,在我的测试中我使用的是Jukito(Guice + Mockito).

Mat*_*son 8

您需要覆盖createRegistry()方法,

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry jndi = super.createRegistry();

    //use jndi.bind to bind your beans

    return jndi;
}

@Test
public void test() {
    //perform test
}
Run Code Online (Sandbox Code Playgroud)