Reputation: 37896
I replaced a configuration which makes a connection to real database with another configuration that should make a connection to in-memory database. I want to use this configuration for integration testing. A simple test looks like this
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class WebApplicationTests {
@Test
public void contextLoads() {}
}
New configuration in pom.xml
<profiles>
<profile>
<id>IntegrationTest</id>
<properties>
<datasource.url>jdbc:h2:mem:db_name_test;MODE=MSSQLServer;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS db_name_test\\;SET SCHEMA db_name_test</datasource.url>
<datasource.username>sa</datasource.username>
<datasource.password></datasource.password>
</properties>
</profile>
<!-- other profiles omitted -->
</profiles>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.195</version>
</dependency>
<!-- some dependencies omitted -->
</dependencies>
These properties are injected in resource files and later picked up by Spring during context initialization. Data Source configuration in XML context looks like:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url" value="${datasource.url}"/>
<property name="username" value="${datasource.username}"/>
<property name="password" value="${datasource.password}"/>
</bean>
I use this command to build the project and perform testing:
mvn -T 1C clean integration-test -P IntegrationTest
The test which worked with previous configuration now throws the below exception.
Stacktrace
2018-03-13 13:36:04.627 ERROR 9076 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@cb6c1e9] to prepare test instance [com.WebApplicationTests@3d52aca]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... some frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'aService': Unsatisfied dependency expressed through field 'ctSecurityUtils'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ctSecurityUtils': Unsatisfied dependency expressed through field 'arService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'arService': Invocation of init method failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... some frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ctSecurityUtils': Unsatisfied dependency expressed through field 'arService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'arService': Invocation of init method failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... some frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'arService': Invocation of init method failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... some frames omitted
Caused by: org.springframework.orm.jpa.JpaSystemException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... some frames omitted
Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
... some frames omitted
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (URL format error; must be "jdbc:h2:{ {.|mem:}[name] | [file:]fileName | {tcp|ssl}:[//]server[:port][,server2[:port]]/name }[;key=value...]" but is "jdbc:h2:mem:db_name_test" [90046-196])
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549) ~[commons-dbcp-1.4.jar:1.4]
... some frames omitted
Caused by: org.h2.jdbc.JdbcSQLException: URL format error; must be "jdbc:h2:{ {.|mem:}[name] | [file:]fileName | {tcp|ssl}:[//]server[:port][,server2[:port]]/name }[;key=value...]" but is "jdbc:h2:mem:db_name_test" [90046-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.196.jar:1.4.196]
... some frames omitted
How to fix it?
Upvotes: 3
Views: 5807
Reputation: 37896
By the trial and error method I've found that this statement in the connection string is causing the error
\\;SET SCHEMA db_name_test
There's no JdbcSQLException: URL format error
exception when it's removed. But I wanted to preserve an application behaviour so instead of deleting the statement I moved it into connectionInitSqls
. Also I've specified driverClassName
.
Updated Maven profile:
<profile>
<id>IntegrationTest</id>
<properties>
<datasource.driverClassName>org.h2.Driver</datasource.driverClassName>
<datasource.url>jdbc:h2:mem:db_name_test;MODE=MSSQLServer;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS db_name_test</datasource.url>
<datasource.connectionInitSqls>SET SCHEMA db_name_test</datasource.connectionInitSqls>
<datasource.username>sa</datasource.username>
<datasource.password></datasource.password>
</properties>
</profile>
Updated Data Source configuration in XML context:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${datasource.driverClassName}"/>
<property name="url" value="${datasource.url}"/>
<property name="username" value="${datasource.username}"/>
<property name="password" value="${datasource.password}"/>
<property name="connectionInitSqls" value="${datasource.connectionInitSqls}"/>
</bean>
Upvotes: 4