Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法拦截和模拟Camel中带有adviceWith的SQL终结点_Java_Apache Camel - Fatal编程技术网

Java 无法拦截和模拟Camel中带有adviceWith的SQL终结点

Java 无法拦截和模拟Camel中带有adviceWith的SQL终结点,java,apache-camel,Java,Apache Camel,我正在尝试使用SQL端点对路由进行单元测试,但由于缺少配置的数据源而失败 这是我的代码: public class TestSqlRouteTest extends CamelTestSupport { @Override public boolean isUseAdviceWith() { return true; } @Override @Before public void setUp() throws Exception

我正在尝试使用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
这来自日志:

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>

编辑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;
    }

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


您可以使用advice来移除/替换路由的部分,而不是使用拦截器。因此,您可以做的是用模拟端点替换sql端点。请参阅文档中有关adviceWith的更多详细信息,了解如何执行此操作:

添加此方法以满足要求:

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

好的,谢谢,但我认为我在代码中就是这么做的。那怎么了?我更改了代码,但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]
@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;
    }