Announcement Announcement Module
Collapse
No announcement yet.
Spring Unit Test with OpenJPA + JNDI Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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:

    <?xml version="1.0" encoding="utf-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="azulstarMuni">
    <provider>org.apache.openjpa.persistence.Persisten ceProviderImpl</provider>
    <jta-data-source>java:comp/env/jdbc/oracleMuni</jta-data-source>
    <class>com.inphonic.azulstar.dto.User</class>
    <class>com.inphonic.azulstar.dto.UserAccount</class>
    <class>com.inphonic.azulstar.dto.UserAddress</class>
    <class>com.inphonic.azulstar.dto.Role</class>
    <class>com.inphonic.azulstar.dto.ServicePlan</class>
    <properties>
    <!--<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>-->
    <property name="openjpa.ConnectionFactoryMode" value="managed"/>
    <property name="openjpa.InverseManager" value="true"/>
    <property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.OracleDictionar y"/>
    <property name="openjpa.Log" value="DefaultLevel=TRACE,SQL=TRACE" />

    <!--<property name="openjpa.ConnectionURL"-->
    <!--value="jdbc:oracle:thin:@//10.2.49.33:1521/lwdb" />-->
    <!--<property name="openjpa.ConnectionDriverName"-->
    <!--value="oracle.jdbc.OracleDriver" />-->
    <!--<property name="openjpa.ConnectionUserName" value="cgi_dev" />-->
    <!--<property name="openjpa.ConnectionPassword" value="cgi_dev" />-->
    <property name="openjpa.jdbc.SynchronizeMappings"
    value="buildSchema(SchemaAction=add)" />

    </properties>
    </persistence-unit>
    </persistence>
    WEB-INF/conf/spring/txManagement.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-2.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schem...ing-tx-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor"/>

    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityMana gerFactoryBean" dependency-check="none">
    <property name="persistenceUnitName" value="azulstarMuni"/>
    </bean>

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

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="userService" class="com.inphonic.azulstar.service.portal.impl.U serServiceImpl">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <bean id="userDao" class="com.inphonic.azulstar.service.dao.impl.DaoI mpl">
    <constructor-arg value="com.inphonic.azulstar.dto.User"/>
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <bean id="roleDao" class="com.inphonic.azulstar.service.dao.impl.DaoI mpl">
    <constructor-arg value="com.inphonic.azulstar.dto.Role"/>
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <bean id="accountDao" class="com.inphonic.azulstar.service.dao.impl.DaoI mpl">
    <constructor-arg value="com.inphonic.azulstar.dto.UserAccount"/>
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    </beans>

    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[] {"classpath*:/WEB-INF/applicationContext.xml",
    "classpath*:/WEB-INF/azulstarMuni-servlet.xml",
    "classpath*:/WEB-INF/conf/spring/azulstar-txManagement.xml"};
    }

    public void testAddUser(){
    User user = (User) userDao.read("a");

    assertEquals("a", 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 <<< FAILURE!
    testAddUser(com.inphonic.azulstar.UserManagerInteg rationTest) Time elapsed: 0.219 sec <<< ERROR!
    org.springframework.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.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.checkDependencies(Abstr actAutowireCapableBeanFactory.java:990)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:805)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireBeanProperties( AbstractAutowireCapableBeanFactory.java:271)
    at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.injectDependencies(AbstractDe pendencyInjectionSpringContextTests.java:179)
    at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.prepareTestInstance(AbstractD ependencyInjectionSpringContextTests.java:158)
    at org.springframework.test.AbstractSingleSpringConte xtTests.setUp(AbstractSingleSpringContextTests.jav a:88)
    at junit.framework.TestCase.runBare(TestCase.java:125 )
    at org.springframework.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.

  • #2
    AbstractTransactionalDataSourceSpringContextTests requires you to have a dataSource, I'd presume you don't have on in your configuration file. You could use AbstractTransactionalSpringContextTests instead.
    http://www.springframework.org/docs/...textTests.html
    Last edited by karldmoore; Aug 29th, 2007, 11:18 AM.

    Comment


    • #3
      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 <<< FAILURE!
      testAddUser(com.inphonic.azulstar.UserManagerInteg rationTest) Time elapsed: 0.207 sec <<< ERROR!
      org.springframework.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.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.checkDependencies(Abstr actAutowireCapableBeanFactory.java:990)
      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:805)
      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireBeanProperties( AbstractAutowireCapableBeanFactory.java:271)
      at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.injectDependencies(AbstractDe pendencyInjectionSpringContextTests.java:179)
      at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.prepareTestInstance(AbstractD ependencyInjectionSpringContextTests.java:158)
      at org.springframework.test.AbstractSingleSpringConte xtTests.setUp(AbstractSingleSpringContextTests.jav a:88)
      at junit.framework.TestCase.runBare(TestCase.java:125 )
      at org.springframework.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)

      Comment


      • #4
        Do you have any working sample?

        Comment


        • #5
          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.
          Last edited by karldmoore; Aug 29th, 2007, 11:18 AM.

          Comment


          • #6
            I'm new to Spring, could you please show me how I can do it..

            Thanks. :0)

            Comment


            • #7
              I have it in txManagement.xml file. Should I move to aplicationContext.xml?

              Comment


              • #8
                I moved everything from txManagement.xml to applicationContext.xml that contains transactionmanager as well, but still got same error.

                Comment


                • #9
                  Anyone, please help.

                  Comment

                  Working...
                  X