|
|
Spring Unit Test with OpenJPA + JNDI
I try to do unit test for spring + openjpa + jndi.
Here are all configuration file:
META-INF/persistence.xml:
lt;?xml version=quot;1.0quot; encoding=quot;utf-8quot;?gt;
lt;persistence xmlns=quot;xml/ns/persistencequot; xmlns:xsi=quot;2001/XMLSchema-instancequot; xsi:schemaLocation=quot;xml/ns/persistence xml/ns/persistence/persistence_1_0.xsdquot; version=quot;1.0quot;gt;
lt;persistence-unit name=quot;azulstarMuniquot;gt; lt;providergt;org.apache.openjpa.persistence.Persisten ceProviderImpllt;/providergt; lt;jta-data-sourcegt;java:comp/env/jdbc/oracleMunilt;/jta-data-sourcegt; lt;classgt;com.inphonic.azulstar.dto.Userlt;/classgt; lt;classgt;com.inphonic.azulstar.dto.UserAccountlt;/classgt; lt;classgt;com.inphonic.azulstar.dto.UserAddresslt;/classgt; lt;classgt;com.inphonic.azulstar.dto.Rolelt;/classgt; lt;classgt;com.inphonic.azulstar.dto.ServicePlanlt;/classgt; lt;propertiesgt;lt;!--lt;property name=quot;openjpa.jdbc.SynchronizeMappingsquot; value=quot;buildSchemaquot;/gt;--gt;lt;property name=quot;openjpa.ConnectionFactoryModequot; value=quot;managedquot;/gt;lt;property name=quot;openjpa.InverseManagerquot; value=quot;truequot;/gt;lt;property name=quot;openjpa.jdbc.DBDictionaryquot; value=quot;org.apache.openjpa.jdbc.sql.OracleDictionar yquot;/gt;lt;property name=quot;openjpa.Logquot; value=quot;DefaultLevel=TRACE,SQL=TRACEquot; /gt;
lt;!--lt;property name=quot;openjpa.Connectionuclquot;--gt;
lt;!--value=quot;jdbc racle:thin //10.2.49.33:1521/lwdbquot; /gt;--gt;
lt;!--lt;property name=quot;openjpa.ConnectionDriverNamequot;--gt;
lt;!--value=quot;oracle.jdbc.OracleDriverquot; /gt;--gt;
lt;!--lt;property name=quot;openjpa.ConnectionUserNamequot; value=quot;cgi_devquot; /gt;--gt;
lt;!--lt;property name=quot;openjpa.ConnectionPasswordquot; value=quot;cgi_devquot; /gt;--gt;
lt;property name=quot;openjpa.jdbc.SynchronizeMappingsquot;
value=quot;buildSchema(SchemaAction=add)quot; /gt;
lt;/propertiesgt; lt;/persistence-unitgt; lt;/persistencegt;
WEB-INF/conf/spring/txManagement.xml:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot; xmlns:xsi=quot;2001/XMLSchema-instancequot; xmlns:aop=quot;schema/aopquot; xmlns:tx=quot;schema/txquot; xsi:schemaLocation=quot; schema/beans schem...-beans-2.0.xsd schema/tx schem...ing-tx-2.0.xsd schema/aop schema/aop/spring-aop-2.0.xsdquot;gt;
lt;bean class=quot;org..orm.jpa.support.Persist enceAnnotationBeanPostProcessorquot;/gt;
lt;bean id=quot;entityManagerFactoryquot; class=quot;org..orm.jpa.LocalEntityMana gerFactoryBeanquot; dependency-check=quot;nonequot;gt; lt;property name=quot;persistenceUnitNamequot; value=quot;azulstarMuniquot;/gt; lt;/beangt;
lt;bean id=quot;transactionManagerquot; class=quot;org..orm.jpa.JpaTransactionM anagerquot;gt; lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt; lt;/beangt;
lt;tx:annotation-driven transaction-manager=quot;transactionManagerquot;/gt;
lt;bean id=quot;userServicequot; class=quot;com.inphonic.azulstar.service.portal.impl.U serServiceImplquot;gt; lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt; lt;/beangt;
lt;bean id=quot;userDaoquot; class=quot;com.inphonic.azulstar.service.dao.impl.DaoI mplquot;gt; lt;constructor-arg value=quot;com.inphonic.azulstar.dto.Userquot;/gt; lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt; lt;/beangt; lt;bean id=quot;roleDaoquot; class=quot;com.inphonic.azulstar.service.dao.impl.DaoI mplquot;gt; lt;constructor-arg value=quot;com.inphonic.azulstar.dto.Rolequot;/gt; lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt; lt;/beangt; lt;bean id=quot;accountDaoquot; class=quot;com.inphonic.azulstar.service.dao.impl.DaoI mplquot;gt; lt;constructor-arg value=quot;com.inphonic.azulstar.dto.UserAccountquot;/gt; lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt; lt;/beangt;
lt;/beansgt;and test code:
public class UserManagerIntegrationTest extends AbstractTransactionalDataSourceSpringContextTests {
private BaseDao userDao;
public void setUserDao(BaseDao userDao) { this.userDao = userDao; }
@Override protected String[] getConfigLocations(){
// setAutowireMode(AUTOWIRE_BY_NAME); return new String[] {quot;classpath*:/WEB-INF/applicationContext.xmlquot;, quot;classpath*:/WEB-INF/azulstarMuni-servlet.xmlquot;, quot;classpath*:/WEB-INF/conf/spring/azulstar-txManagement.xmlquot;}; }
public void testAddUser(){ User user = (User) userDao.read(quot;aquot;);
assertEquals(quot;aquot;, user.getUserId()); }
}
Application is running fine, but the unit test is not able to run, here is error message:
-------------------------------------------------------------------------------
Test set: com.inphonic.azulstar.UserManagerIntegrationTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.269 sec lt;lt;lt; FAILURE!
testAddUser(com.inphonic.azulstar.UserManagerInteg rationTest) Time elapsed: 0.219 sec lt;lt;lt; ERROR!
org..beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'com.inphonic.azulstar.UserManagerIntegrationTest' : Unsatisfied dependency expressed through bean property 'dataSource': Set this property value or disable dependency checking for this bean.
at org..beans.factory.support.Abstract AutowireCapableBeanFactory.checkDependencies(Abstr actAutowireCapableBeanFactory.java:990)
at org..beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:805)
at org..beans.factory.support.Abstract AutowireCapableBeanFactory.autowireBeanProperties( AbstractAutowireCapableBeanFactory.java:271)
at org..test.AbstractDependencyInjecti onSpringContextTests.injectDependencies(AbstractDe pendencyInjectionSpringContextTests.java:179)
at org..test.AbstractDependencyInjecti onSpringContextTests.prepareTestInstance(AbstractD ependencyInjectionSpringContextTests.java:158)
at org..test.AbstractSingleSpringConte xtTests.setUp(AbstractSingleSpringContextTests.jav a:88)
at junit.framework.TestCase.runBare(TestCase.java:125 )
at org..test.ConditionalTestCase.runBa re(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.ja va:106)
at junit.framework.TestResult.runProtected(TestResult .java:124)
at junit.framework.TestResult.run(TestResult.java:109 )
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
at junit.framework.TestSuite.run(TestSuite.java:203)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.junit.JUnitTestSet.execu te(JUnitTestSet.java:213)
at org.apache.maven.surefire.suite.AbstractDirectoryT estSuite.executeTestSet(AbstractDirectoryTestSuite .java:138)
at org.apache.maven.surefire.suite.AbstractDirectoryT estSuite.execute(AbstractDirectoryTestSuite.java:1 25)
at org.apache.maven.surefire.Surefire.run(Surefire.ja va:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.booter.SurefireBooter.ru nSuitesInProcess(SurefireBooter.java:290)
at org.apache.maven.surefire.booter.SurefireBooter.ma in(SurefireBooter.java:818)
at org.apache.maven.surefire.booter.SurefireBooter.ma in(SurefireBooter.java:818)
Question is,
Is it possible to do unit test against on JNDI?
If not, how I can test this?
Please give your advise.
Thanks in advance.AbstractTransactionalDataSourceSpringContextTests requires you to have a dataSource, I'd presume you don't have on in your configuration file. You could use AbstractTransactionalSpringContextTests instead.
docs/...textTests.html
Thanks Karldmoore, I changed to AbstractTransactionalSpringContextTests, but I got following error message:
It looks like spring not able to load beans from xml file.
-------------------------------------------------------------------------------
Test set: com.inphonic.azulstar.UserManagerIntegrationTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.262 sec lt;lt;lt; FAILURE!
testAddUser(com.inphonic.azulstar.UserManagerInteg rationTest) Time elapsed: 0.207 sec lt;lt;lt; ERROR!
org..beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'com.inphonic.azulstar.UserManagerIntegrationTest' : Unsatisfied dependency expressed through bean property 'transactionManager': Set this property value or disable dependency checking for this bean.
at org..beans.factory.support.Abstract AutowireCapableBeanFactory.checkDependencies(Abstr actAutowireCapableBeanFactory.java:990)
at org..beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:805)
at org..beans.factory.support.Abstract AutowireCapableBeanFactory.autowireBeanProperties( AbstractAutowireCapableBeanFactory.java:271)
at org..test.AbstractDependencyInjecti onSpringContextTests.injectDependencies(AbstractDe pendencyInjectionSpringContextTests.java:179)
at org..test.AbstractDependencyInjecti onSpringContextTests.prepareTestInstance(AbstractD ependencyInjectionSpringContextTests.java:158)
at org..test.AbstractSingleSpringConte xtTests.setUp(AbstractSingleSpringContextTests.jav a:88)
at junit.framework.TestCase.runBare(TestCase.java:125 )
at org..test.ConditionalTestCase.runBa re(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.ja va:106)
at junit.framework.TestResult.runProtected(TestResult .java:124)
at junit.framework.TestResult.run(TestResult.java:109 )
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
at junit.framework.TestSuite.run(TestSuite.java:203)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.junit.JUnitTestSet.execu te(JUnitTestSet.java:213)
at org.apache.maven.surefire.suite.AbstractDirectoryT estSuite.executeTestSet(AbstractDirectoryTestSuite .java:138)
at org.apache.maven.surefire.suite.AbstractDirectoryT estSuite.execute(AbstractDirectoryTestSuite.java:1 25)
at org.apache.maven.surefire.Surefire.run(Surefire.ja va:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.booter.SurefireBooter.ru nSuitesInProcess(SurefireBooter.java:290)
at org.apache.maven.surefire.booter.SurefireBooter.ma in(SurefireBooter.java:818)
at org.apache.maven.surefire.booter.SurefireBooter.ma in(SurefireBooter.java:818)
Do you have any working sample?
I'd guess that you must not be loading an applicationContext file that contains the transactionManager. The spring test does dependency checking so the transactionManager must be set.
I'm new to Spring, could you please show me how I can do it..
Thanks. :0)
I have it in txManagement.xml file. Should I move to aplicationContext.xml?
I moved everything from txManagement.xml to applicationContext.xml that contains transactionmanager as well, but still got same error.
Anyone, please help. |
|