2017-10-16 65 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