简体   繁体   中英

Testing camel-sql route with in-memory database not fetching results

I have written the code using camel-sql which is working fine. Now I have to write test cases for the same. I have used in-memory database H2. I have initialized the database and assigned the datasource to sqlComponent.

 // 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");

The problem is, as soon as the test case starts, it is saying

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

I looked into camel-SQL component test cases and doing the same thing but the code is not able to find dataSource. Please help. Thanks in advance.

After spending a lot of time on this issue, I identified that H2 database was using JDBCUtils to fetch records and It was throwing ClassNotFoundException. I was getting it nowhere in Camel exception hierarchy because this exception was being suppressed and all I was getting a generic exception message. Here is the exception:

ClassNotFoundException: com.vividsolutions.jts.geom.Geometry

After searching for the issue I found out that It requires one more dependency. So I added it and it resolved the issue.

Issue URL: https://github.com/elastic/elasticsearch/issues/9891

Dependency: https://mvnrepository.com/artifact/com.vividsolutions/jts-core/1.14.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM