Eugen
Eugen

Reputation: 1543

Switching Databases when testing Spring MVC web application?

The Spring project (in its production form) will be using a MySQL database. I want the tests ran on the project to use HYPERSONIC_IN_MEMORY.

I've added the following new files in the test/resources folder.

However, the project throws a MYSQL syntax error (it's trying to use MySQL with HYPERSONIC somehow).

Considering the fact that I'm a total beginner (first project in Spring), I'd like to know what the steps are to switching databases for testing (what I might be doing wrong).

LE: This is what I'm getting when running maven tests:

Tests run: 9, Failures: 0, Errors: 9, Skipped: 0, Time elapsed: 12.29 sec <<< FAILURE!
testCountAllAccountAllocations(com.mms.pone.portal.domain.AccountAllocationIntegrationTest)  Time elapsed: 11.813 sec  <<< ERROR!
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Cannot open connection
    at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:427)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.startTransaction(TransactionalTestExecutionListener.java:513)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener.startNewTransaction(TransactionalTestExecutionListener.java:271)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:164)
    at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:358)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:172)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:104)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:70)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Cannot open connection
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315)
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1397)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:63)
    at org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction(DefaultJpaDialect.java:70)
    at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:377)
    ... 31 more
Caused by: org.hibernate.exception.GenericJDBCException: Cannot open connection

And a lot more of those. I get the feeling that maven isn't using the new xml files and still sticking to the old ones.

Any way I can figure that out?

Upvotes: 0

Views: 1186

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328556

Search for org.hibernate.dialect.MySQLDialect and replace it with org.hibernate.dialect.HSQLDialect (docs)

[EDIT] The error says "Cannot open connection". Check the JDBC URL, user+password and for more errors further up in the output.

Upvotes: 2

Sashi
Sashi

Reputation: 2425

I'm assuming this is how your original applicationContext.xml looked (also assuming that the persistence.xml file defines a persitence unit with jndi name 'testEM')

<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:comp/env/testEM" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

For testing purposes, you basically need to override how entityManagerFactory is created. In your applicationContext-test.xml file override 'entityManagerFactory' bean definition as shown in the snippet below. Create a 'META-INF/test-persistence.xml' in src/test/resources that creates a connection to hsql in-memory db.

test-persistence.xml

    <persistence-unit name="testPu">
    <properties>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.password" value=""/>
        <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:schemaname"/>
        <property name="hibernate.connection.username" value="sa"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
    </properties>
</persistence-unit>

applicationContext-test.xml

    <!--  Import the original applicationContext and override properties for test purposes -->
<import resource="classpath:applicationContext" />

<!--  In our case, we need to override entityManagerFactory definition -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml" />
    <!-- other properties (omitted) -->
</bean>

Upvotes: 1

Related Questions