Search code examples
springmulemule4munit

Mule 4 trying to initialise a spring based H2 datasource in Munit - getting ClassNotFoundException


I am trying to write a Munit in Mule 4 where in Munit I want to use a Spring based H2 datasource

So I have the following defined in my Munit class :

<munit:config name="munit-test-config.xml" />
    <spring:config name="Spring_Config" doc:name="Spring Config" files="beans.xml" >        
    </spring:config>
    
    <db:config name="Database_Config" doc:name="Database Config">
        <db:data-source-connection dataSourceRef="dataSource" />
    </db:config>

In beans.xml :

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="org.h2.Driver" />
            <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
            <property name="username" value="sa" />
            <property name="password" value="" />
        </bean>

In my pom have included required dependencies and as a shared library :

<dependency> 
    <groupId>com.mchange</groupId> 
    <artifactId>c3p0</artifactId> 
    <version>0.9.5.2</version>
    <scope>test</scope> 
</dependency>

<sharedLibrary> 
    <groupId>com.mchange</groupId> 
    <artifactId>c3p0</artifactId> 
</sharedLibrary>

However if I run the application via anypoint studio it runs fine , BUT if I try and run the Munit it fails with :

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [beans.xml]; nested exception is org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException: Cannot load class 'org.apache.commons.dbcp.BasicDataSource': [ org.apache.commons.dbcp.BasicDataSource, Cannot load class 'org.apache.commons.dbcp.BasicDataSource': [

what am I missing ?


Solution

  • Your pom declares a dependency for the c3p0 database connection pool but your Spring bean creates a pool using the Apache DBCP database connection pool. Either use DBCP or c3p0 in both places.