2017-10-16 64 views
0

我已经使用camel-sql编写的代码工作正常。现在我必须编写相同的测试用例。我使用了内存数据库H2。我已初始化数据库并将数据源分配给sqlComponent。使用内存数据库测试camel-sql路由不获取结果

// Setup code 
    @Override 
    protected JndiRegistry createRegistry() throws Exception { 
     JndiRegistry jndi = super.createRegistry(); 

     // this is the database we create with some initial data for our unit test 
     database = new EmbeddedDatabaseBuilder() 
       .setType(EmbeddedDatabaseType.H2).addScript("createTableAndInsert.sql").build(); 

     jndi.bind("myDataSource", database); 
     return jndi; 
    } 

    // Testcase code 
    @SuppressWarnings("unchecked") 
    @Test 
    public void testRoute() throws Exception { 

     Exchange receivedExchange = template.send("direct:myRoute", ExchangePattern.InOut ,exchange -> { 
      exchange.getIn().setHeader("ID", new Integer(1)); 
     }); 

     camelContext.start(); 
     MyClass updatedEntity = (MyClass)jdbcTemplate.queryForObject("select * from MY_TABLE where id=?", new Long[] { 1l } , 
       new RouteTest.CustomerRowMapper()); 
     // Here I can get the updatedEntity from jdbcTemplate 
     assertNotNull(receivedExchange); 
     assertNotNull(updatedEntity); 
    } 

    // Main code 
    from("direct:myRoute") 
    .routeId("pollDbRoute") 
     .transacted() 
     .to("sql:select * from MY_TABLE msg where msg.id = :#"+ID+"?dataSource=#myDataSource&outputType=SelectOne&outputClass=com.entity.MyClass") 
     .log(LoggingLevel.INFO,"Polled message from DB"); 

的问题是,一旦测试案例开始,跟它

No bean could be found in the registry for: myDataSource of type: javax.sql.DataSource 

我看着骆驼SQL组件的测试案例,做同样的事情,但代码不能找到dataSource。请帮忙。提前致谢。

+0

你能添加你的测试的源代码? – ltsallas

+0

尝试查看camel-sql本身的单元测试,因为它使用内存数据库进行测试 –

回答

0

在这个问题上花了很多时间后,我发现H2数据库正在使用JDBCUtils来获取记录,并抛出了ClassNotFoundException异常。我在Camel异常层次结构中找不到任何地方,因为这个异常被压制,我得到一个通用的异常消息。这里是例外:

ClassNotFoundException: com.vividsolutions.jts.geom.Geometry 

在搜索问题后,我发现它需要一个更多的依赖关系。所以我添加了它并解决了问题。

问题URL:https://github.com/elastic/elasticsearch/issues/9891

相关性:https://mvnrepository.com/artifact/com.vividsolutions/jts-core/1.14.0