[英]Unable to intercept and mock SQL endpoint with adviceWith in Camel
I'm trying to unit test a route with a SQL endpoint but fail due to lack of configured Datasource. 我正在尝试对具有SQL端点的路由进行单元测试,但由于缺少已配置的数据源而失败。
This is my code: 这是我的代码:
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();
}
}
The error I get is 我得到的错误是
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
I'm using Camel 2.22.0 and Java 8. 我正在使用Camel 2.22.0和Java 8。
Please advice (pardon the pun) 请指教(对双关语)
M 中号
EDIT : Added this change with the same result: SQL component gets started and expecting Datasource. 编辑 :添加此更改具有相同的结果:SQL组件启动并期望数据源。
Added some more components, jms, http and they are mocked perfectly… 添加了更多组件,jms,http,它们得到了完美的模拟……
@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();
}
});
}
This is from the log: 这是从日志:
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>
As far as I can see the SQL component is never adviced: 据我所知,从不建议使用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]
EDIT 2: Added this to the class and now I can mock SQL component… 编辑2:将其添加到类中,现在我可以模拟SQL组件…
This datasource is never used, only for requirements… 此数据源从未使用过,仅用于需求…
@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;
}
When you use interceptors like in your unit test, then the component/endpoint must be valid to be able to startup. 当您在单元测试中使用拦截器时,组件/端点必须有效才能启动。 You would also need the component on the classpath etc.
您还需要在classpath等上使用该组件。
Instead of using interceptors you can use advice with, to remove / replace parts of the route. 可以使用建议来代替/使用拦截器,以删除/替换部分路线。 So what you can do is to replace the sql endpoint with a mock endpoint.
因此,您可以做的是用模拟端点替换sql端点。 See more details about adviceWith in the documentation for how to do that: http://camel.apache.org/advicewith.html
有关如何执行操作的更多信息,请参见文档: http ://camel.apache.org/advicewith.html
Add this method to fulfill the requirements: 添加此方法以满足要求:
@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;
}
The datasource can be anything valid in pom, I chose h2 since it's simple… The datasource is never used in my example. 数据源可以是pom中任何有效的数据,我选择了h2,因为它很简单……在我的示例中从未使用过数据源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.