骆驼@BeanInject 在蓝图测试中不起作用
Camel @BeanInject doesn't work in Blueprint-Test
我正在尝试使用 CamelBlueprintTestSupport
来测试应用多个 bean 的骆驼路线。在生产代码中,bean 在单独的蓝图中定义 XML。在测试 class 中,我像记录的那样覆盖了 addServicesOnStartup
方法。我的单元测试 class 看起来像这样:
public class MyUnit_Test extends CamelBlueprintTestSupport {
@Override
protected String getBlueprintDescriptor() {
return "/OSGI-INF/blueprint/camel-routes.xml";
}
@Override
protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) {
services.put("beanA", asService(new BeanA(), null, null));
services.put("beanB", asService(new BeanB(), null, null));
}
@EndpointInject(uri = "mock:endpointMock")
private MockEndpoint endpointMoc;
@Test
public void test_myRoute() throws Exception {
(test code)
}
}
路由中引用了一个 bean (BeanA
):
<camelContext>
<route>
...
<bean ref="beanA" method="myMethod" />
...
</route>
</camelContext>
这个需要访问另一个 bean (BeanB
)。为此,我在 BeanA
中使用了 @BeanInject
注释:
public class BeanA {
@BeanInject("beanB")
private BeanB beanB;
public void myMethod(Exchange exchange) {
beanB.getSomething();
...
}
在生产环境中,依赖注入运行良好。
单元测试中的问题
在单元测试中BeanA
可以通过路由引用。但是在BeanA
里面注入BeanB
好像不行。当代码尝试访问 beanB
.
时,我在 BeanA.myMethod()
收到 NPE
似乎依赖注入只对蓝图中的路由有效XML,但对注入的 bean 本身无效。
尝试次数
我试图覆盖 CamelTestSupport 的 createRegistry
方法并通过这种方式添加我的 bean。但它没有用。事实上,我发现 createRegistry
无论如何都不会在 CamelBlueprintTestSupport
启动时被调用。
在 Camel documentation for Blueprint testing 中,他们将 PojoSR
称为注册表框架。但是我找不到更多示例或说明如何根据我的需要使用它。
是的,这是行不通的。我的理解是 服务注册表 中的内容与 蓝图上下文 中的 beans 不同。
Camel 将尝试使用(Camel,而非蓝图)注解 @InjectBean 从 context 自动装配其 bean。但是 Camel 不会 fiddle 使用 服务注册表 中的内容;这些是 ready-made 要使用的对象 as-is 或通过代理。
因此您可以:
- 将 beanA 视为服务;然后你准备好它(手动将beanB注入其中)并将它添加到服务注册表中;
- 通过为此目的修改 blueprint.xml,将 beanA 和 beanB 放入蓝图上下文中(就像您在生产代码中所做的那样)。 (不过我知道你可能不想那样做。)
我们需要的是一个 CamelBlueprintTestSupport,它会考虑多个蓝图上下文,但据我所知这不可用。事实上,在蓝图中不可能 import 上下文(与 Spring 相反。)
我正在尝试使用 CamelBlueprintTestSupport
来测试应用多个 bean 的骆驼路线。在生产代码中,bean 在单独的蓝图中定义 XML。在测试 class 中,我像记录的那样覆盖了 addServicesOnStartup
方法。我的单元测试 class 看起来像这样:
public class MyUnit_Test extends CamelBlueprintTestSupport {
@Override
protected String getBlueprintDescriptor() {
return "/OSGI-INF/blueprint/camel-routes.xml";
}
@Override
protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) {
services.put("beanA", asService(new BeanA(), null, null));
services.put("beanB", asService(new BeanB(), null, null));
}
@EndpointInject(uri = "mock:endpointMock")
private MockEndpoint endpointMoc;
@Test
public void test_myRoute() throws Exception {
(test code)
}
}
路由中引用了一个 bean (BeanA
):
<camelContext>
<route>
...
<bean ref="beanA" method="myMethod" />
...
</route>
</camelContext>
这个需要访问另一个 bean (BeanB
)。为此,我在 BeanA
中使用了 @BeanInject
注释:
public class BeanA {
@BeanInject("beanB")
private BeanB beanB;
public void myMethod(Exchange exchange) {
beanB.getSomething();
...
}
在生产环境中,依赖注入运行良好。
单元测试中的问题
在单元测试中BeanA
可以通过路由引用。但是在BeanA
里面注入BeanB
好像不行。当代码尝试访问 beanB
.
BeanA.myMethod()
收到 NPE
似乎依赖注入只对蓝图中的路由有效XML,但对注入的 bean 本身无效。
尝试次数
我试图覆盖 CamelTestSupport 的
createRegistry
方法并通过这种方式添加我的 bean。但它没有用。事实上,我发现createRegistry
无论如何都不会在CamelBlueprintTestSupport
启动时被调用。在 Camel documentation for Blueprint testing 中,他们将
PojoSR
称为注册表框架。但是我找不到更多示例或说明如何根据我的需要使用它。
是的,这是行不通的。我的理解是 服务注册表 中的内容与 蓝图上下文 中的 beans 不同。
Camel 将尝试使用(Camel,而非蓝图)注解 @InjectBean 从 context 自动装配其 bean。但是 Camel 不会 fiddle 使用 服务注册表 中的内容;这些是 ready-made 要使用的对象 as-is 或通过代理。
因此您可以:
- 将 beanA 视为服务;然后你准备好它(手动将beanB注入其中)并将它添加到服务注册表中;
- 通过为此目的修改 blueprint.xml,将 beanA 和 beanB 放入蓝图上下文中(就像您在生产代码中所做的那样)。 (不过我知道你可能不想那样做。)
我们需要的是一个 CamelBlueprintTestSupport,它会考虑多个蓝图上下文,但据我所知这不可用。事实上,在蓝图中不可能 import 上下文(与 Spring 相反。)