java - 无法使用 Camel 中的 adviceWith 拦截和模拟 SQL 端点
问题描述
我正在尝试使用 SQL 端点对路由进行单元测试,但由于缺少配置的数据源而失败。
这是我的代码:
public class TestSqlRouteTest extends CamelTestSupport {
@Override
public boolean isUseAdviceWith() {
return true;
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("direct:sql").routeId("directsql").to("sql://select 1 from DUAL").end();
}
});
context.getRouteDefinition("directsql")
.adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() {
interceptSendToEndpoint("sql:*").to("mock:sql").skipSendToOriginalEndpoint();
}
});
}
/**
* Test sql.
*
* @throws Exception the exception
*/
@Test
public void testSQL() throws Exception {
context.start();
template.sendBody("mock:sql", "body");
final MockEndpoint mockSQL = getMockEndpoint("mock:sql");
mockSQL.expectedMessageCount(1);
mockSQL.assertIsSatisfied();
assertMockEndpointsSatisfied();
context.stop();
}
}
我得到的错误是
org.apache.camel.FailedToCreateRouteException: Failed to create route directsql at: >>> To[sql://select 1 from DUAL] <<< in route: Route(directsql)[[From[direct:sql]] -> [To[sql://select 1 fr... because of Failed to resolve endpoint: sql://select%201%20from%20DUAL due to: DataSource must be configured
我正在使用 Camel 2.22.0 和 Java 8。
请指教(原谅双关语)
米
编辑:添加了此更改,结果相同:SQL 组件启动并期待数据源。
添加了更多组件,jms,http,它们被完美地模拟了......</p>
@Override
@Before
public void setUp() throws Exception {
super.setUp();
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("direct:sql")
.routeId("directsql")
.to("jms:queue:whatever")
.to("http://www.whatever.com")
.to("sql://select 1 from DUAL")
.end();
}
});
context.getRouteDefinition("directsql")
.adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
mockEndpoints();
}
});
}
这是来自日志:
2018-07-19 14:21:03.465 INFO --- [ main] org.apache.camel.model.RouteDefinition : Adviced route before/after as XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<route xmlns="http://camel.apache.org/schema/spring" customId="true" id="directsql">
<from uri="direct:sql"/>
<to uri="jms:queue:whatever"/>
<to uri="http://www.whatever.com"/>
<to uri="sql://select 1 from DUAL"/>
</route>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<route xmlns="http://camel.apache.org/schema/spring" customId="true" id="directsql">
<from uri="direct:sql"/>
<to uri="jms:queue:whatever"/>
<to uri="http://www.whatever.com"/>
<to uri="sql://select 1 from DUAL"/>
</route>
据我所知,从未建议使用 SQL 组件:
2018-07-19 14:21:03.488 INFO --- [ main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [direct://sql] with mock endpoint [mock:direct:sql]
…
2018-07-19 14:21:03.541 INFO --- [ main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [jms://queue:whatever] with mock endpoint [mock:jms:queue:whatever]
…
2018-07-19 14:21:03.634 INFO --- [ main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [http://www.whatever.com] with mock endpoint [mock:http:www.whatever.com]
编辑 2: 将其添加到类中,现在我可以模拟 SQL 组件......</p>
此数据源从未使用过,仅用于需求……</p>
@Override
protected JndiRegistry createRegistry() throws Exception {
final JndiRegistry jndi = super.createRegistry();
final BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.h2.Driver");
ds.setUrl("jdbc:h2:mem:default");
jndi.bind("dataSource", ds);
return jndi;
}
解决方案
添加此方法以满足要求:
@Override
protected JndiRegistry createRegistry() throws Exception {
final JndiRegistry jndi = super.createRegistry();
final BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.h2.Driver");
ds.setUrl("jdbc:h2:mem:default");
jndi.bind("dataSource", ds);
return jndi;
}
数据源可以是任何在 pom 中有效的东西,我选择了 h2,因为它很简单……在我的示例中从未使用过数据源。
推荐阅读
- php - Xdebug 在 PhpStorm 上不起作用 - PHP 7.2.10,Ubuntu 18.04.01
- firebase - 阵列操作的 Cloud Firestore 安全规则
- ember.js - 如何确定调用函数的位置?
- c# - 如何在 C# 中使用 Azure Functions 读取 Blob 输入绑定中的 XML 文件?
- node.js - 更改 JSON 文件路径并从外部加载到内部
- java - Mockito:如何模拟functionToBeTested中另一个类的函数行为?
- python - 检查 False 作为字典中的任何值
- python - python数字生成器可能的无限循环
- c++ - g++ 关于零文字的不一致
- javascript - Angular 6 +从文档中获取具有类名的元素并更改样式