无法在骆驼中使用adviceWith拦截和模拟SQL端点

时间:2018-07-19 07:03:48

标签: java apache-camel

我正在尝试对带有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。

请咨询(对双关语)

M

编辑:添加了此更改,结果相同:SQL组件启动并期待数据源。

添加了更多组件,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();
                    }
                });
    }

这是从日志中获取的:

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组件了……

从不使用此数据源,仅用于要求...

@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;
    }

2 个答案:

答案 0 :(得分:0)

在单元测试中使用拦截器时,组件/端点必须有效才能启动。您还需要在类路径等上添加该组件。

除了使用拦截器外,还可以使用建议来删除/替换路线的某些部分。因此,您可以做的是用模拟端点替换sql端点。有关如何执行操作的更多信息,请参见文档:http://camel.apache.org/advicewith.html

答案 1 :(得分:0)

添加此方法以满足要求:

@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,因为它很简单……示例中从未使用过数据源。