Announcement Announcement Module
Collapse
No announcement yet.
begginers problem: can not get AbstractJpaTests running Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • begginers problem: can not get AbstractJpaTests running

    Hi,

    I'm trying to run a simple test case that extends AbstractJpaTests:

    Code:
    import org.springframework.test.jpa.AbstractJpaTests;
    
    public class EmployeeServiceIntegrationTest extends AbstractJpaTests {
    	private EmployeeService employeeService;
    	private long JoeSmithId = 99999;
    
    	public void setEmployeeService(EmployeeService employeeService) {
    		this.employeeService = employeeService;
    	}
    
    	protected String[] getConfigLocations() {
    		return new String[] { "classpath:/com/ibm/dw/spring2/dwspring2-service.xml" };
    	}
    
    	public void testFindByEmployeeId() {
    		Employee emp = employeeService.findById(JoeSmithId);
    		assertNotNull(emp);
    		assertEquals("Joe", emp.getFirstName());
    	}
    }
    When I run the test, I get the following exception:

    Code:
    java.lang.IllegalArgumentException: Can not set java.lang.Object field org.springframework.test.jpa.AbstractJpaTests.shadowParent to com.ibm.dw.spring2.EmployeeServiceIntegrationTest
    	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
    	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
    	at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:37)
    	at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:57)
    	at java.lang.reflect.Field.set(Field.java:657)
    	at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:246)
    	at junit.framework.TestResult$1.protect(TestResult.java: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:208)
    	at junit.framework.TestSuite.run(TestSuite.java:203)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    Any ideas what's wrong here? I use spring version 2.5.5.

    Thanks,
    Marius

  • #2
    hi,

    please post more code from your DAO-class, Entity Bean, and configuration

    regards,

    Nikolai

    Comment


    • #3
      Here is my Employee POJO:

      Code:
      package com.ibm.dw.spring2;
      
      import java.util.Date;
      
      import javax.persistence.*;
      
      @Entity
      public class Employee {
      
      	@Id
      	@GeneratedValue(strategy = GenerationType.TABLE)
      	private long empid;
      
      	@Column(length = 6)
      	private String empno;
      
      	@Column(name = "FIRSTNME")
      	private String firstName;
      
      	@Column(name = "MIDINIT")
      	private String midInitial;
      
      	@Column(name = "LASTNME")
      	private String lastName;
      
      	@Column(name = "PHONENO")
      	private String phoneNumber;
      
              // Getter setter methods
              // ...
      
      	public Employee(String empno, String firstName, String midInitial,
      			String lastName, String phoneNumber) {
      		this.empno = empno;
      		this.firstName = firstName;
      		this.midInitial = midInitial;
      		this.lastName = lastName;
      		this.phoneNumber = phoneNumber;
      	}
      
      	public Employee() {
      	}
      }
      EmployeeDAO:

      Code:
      package com.ibm.dw.spring2;
      
      import org.springframework.orm.jpa.support.JpaDaoSupport;
      
      public class EmployeeDAO extends JpaDaoSupport  {
      
      	public Employee findById(long id) {
      		return getJpaTemplate().find(Employee.class, id);
      	}
      }
      Configuration (dwspring2-service.xml file):

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans>
         <!--
            bean element here is without namespace and scheme because I can not 
            include URL's in my posts
         -->
         <bean id="employeeService" class="com.ibm.dw.spring2.EmployeeDAO">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
         </bean>
         
         <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter">
               <bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
                  <property name="showSql" value="true"/>
                  <property name="generateDdl" value="true"/>
                 
                  <property name="databasePlatform" value="oracle.toplink.essentials.platform.database.HSQLPlatform"/>
               </bean>
            </property>
            <property name="loadTimeWeaver">
               <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
            </property>
         </bean>
      
         <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
            <property name="url" value="jdbc:hsqldb:mem:dwspring"/>
            <property name="username" value="sa" />
            <property name="password" value="" />
         </bean>
      
         <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
            <property name="dataSource" ref="dataSource"/>
         </bean>
      
      </beans>
      my persistence.xml file:

      Code:
      <persistence>
          <persistence-unit name="dwSpring2Jpa" transaction-type="RESOURCE_LOCAL"/>
      </persistence>

      Comment


      • #4
        Hi,

        i have changed following in your code (EmployeeService to DAO), and junit runs without faults:

        in your test class:
        Code:
                private EmployeeDAO dao;
        	private long JoeSmithId = 99999;
        
        	public void setEmployeeDAO(EmployeeDAO dao) {
        		this.dao = dao;
        	}
                public void testFindByEmployeeId() {
        		Employee joeSmith = new Employee();
        		joeSmith.setFirstName("Joe");
        		joeSmith.setLastName("Smith");
        		dao.saveEmployee(joeSmith);
        		
        		
        		assertTrue("primary key assigned", joeSmith.getEmpid() != null);
        		long id = joeSmith.getEmpid(); 
        		Employee emp = dao.findById(id);
        		assertNotNull(emp);
        		assertEquals("Joe", emp.getFirstName());
        	}
        and in your configuration

        Code:
        <bean id="employeeDAO" class="_yourDAOJPAPackage_.EmployeeDAOJPA">
              <property name="entityManagerFactory" ref="entityManagerFactory"/>
           </bean>
        regards,

        Nikolai

        Comment


        • #5
          Nikolai,

          It doesn't work for me though. Maybe you forgot to mention some changes?
          I attached complete eclipse project to the post.

          Marius

          Comment


          • #6
            need help in making a simple spring project

            Hi All,

            I am able to run a simple application in spring framework .
            But my problem is i m not able to retreive value submitted from Form to te result jsp...

            It is redirecting to result jsp but there is printing " " ...

            Plz help me in this regard.

            Also can any body tell me how to post a new thread ....for my querries...

            Thanks a lot in advance.....

            Comment


            • #7
              Originally posted by msemenas View Post
              Nikolai,

              It doesn't work for me though. Maybe you forgot to mention some changes?
              I attached complete eclipse project to the post.

              Marius
              Hi Marius,

              i have changed your TestClass to "extended TestCase" and now i get a "java.lang.IllegalArgumentException: Object: com.ibm.dw.spring2.Employee@1797795 is not a known entity type."

              Code:
              public class EmployeeServiceIntegrationTest extends TestCase {
              	private EmployeeDAO dao;
              	
              	private ApplicationContext ctx = null;
              	
              	public EmployeeServiceIntegrationTest(){
              		String[] paths = {"classpath:/com/ibm/dw/spring2/dwspring2-service.xml"};
              		ctx = new ClassPathXmlApplicationContext(paths);
              	}
              
              	public void setEmployeeDAO(EmployeeDAO dao) {
              		this.dao = dao;
              	}
              	
              	protected void setUp() throws Exception {
              		dao = (EmployeeDAO) ctx.getBean("employeeDAO");
              	}
              	
              
              	public void testFindByEmployeeId() {
              		Employee joeSmith = new Employee();
              		joeSmith.setFirstName("Joe");
              		joeSmith.setLastName("Smith");
              		dao.save(joeSmith);
              
              		assertTrue("primary key assigned", joeSmith.getEmpid() != null);
              		long id = joeSmith.getEmpid();
              		Employee emp = dao.findById(id);
              		assertNotNull(emp);
              		assertEquals("Joe", emp.getFirstName());
              	}
              	
              //	protected String[] getConfigLocations() {
              //		return new String[] { "classpath:/com/ibm/dw/spring2/dwspring2-service.xml" };
              //	}
              	
              	public static void main(String[] args) {
              		junit.textui.TestRunner.run(EmployeeServiceIntegrationTest.class);
              	}
              	
              }
              sorry, but i think, that your configuration, or your JPA-implementation have a problem.

              I use Hibernate as JPA provider and have no problem. You can see my project in attachment.

              If you want to run my example, you need a lot of libraries)

              regards, Nikolai

              Comment


              • #8
                Thanks Nikolai,

                I just fixed my own version.
                I configured my test class to don't use shadow class loader:

                Code:
                protected boolean shouldUseShadowLoader() {
                  return false;
                }
                Also I changed loadTimeWeaver property of entityManagerFactory to another class:

                Code:
                <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                  <property name="loadTimeWeaver">
                    <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
                  </property>
                </bean>
                Now to run my test case I need to supply the following JVM option:
                Code:
                -javaagent:/path/to/spring-agent.jar
                It seems quite complicated for such a simple task I'll try Hibernate as JPA provider. Maybe it will make life simplier

                regards,
                Marius

                Comment


                • #9
                  The solution works!

                  I wonder why changing the classloader setting and adding the javaagent should make it work?

                  Comment


                  • #10
                    I just found that exception is thrown at org.springframework.test.jpa.AbstractJpaTests.runB are(AbstractJpaTests.java:246) because some classes are loaded by shadow class loader. When I disabled shadow loader the problem disappeared.
                    I configured LocalContainerEntityManagerFactoryBean as it is described in Spring documentation (chapter 12.6.1.3. LocalContainerEntityManagerFactoryBean). There is written why VM agent is required.
                    I haven't a deep knowledge of spring framework. I just found a solution that works for me

                    Marius

                    Comment


                    • #11
                      not able to execute unit test for spring JPA

                      org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'employeeService' defined in class path resource [com/ibm/dw/spring2/dwspring2-service.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/ibm/dw/spring2/dwspring2-service.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.
                      at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:275)
                      at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:104)


                      I have seen other thread and try to test , i have run app from Eclipse ,

                      here is the contecxt def

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

                      <bean id="employeeService" class="com.ibm.dw.spring2.EmployeeDAO">
                      <property name="entityManagerFactory" ref="entityManagerFactory" />
                      </bean>

                      <bean id="entityManagerFactory"
                      class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
                      <property name="loadTimeWeaver">
                      <bean
                      class="org.springframework.instrument.classloading .InstrumentationLoadTimeWeaver" />
                      </property>

                      <property name="dataSource" ref="dataSource" />
                      <property name="jpaVendorAdapter">
                      <bean class="org.springframework.orm.jpa.vendor.TopLinkJ paVendorAdapter">
                      <property name="showSql" value="true" />
                      <property name="generateDdl" value="true" />

                      <property name="databasePlatform"
                      value="oracle.toplink.essentials.platform.database .PostgreSQLPlatform" />
                      <!-- <property name="databasePlatform" value="oracle.toplink.essentials.platform.database .DB2Platform"/> -->
                      </bean>
                      </property><!--
                      <property name="loadTimeWeaver">
                      <bean
                      class="org.springframework.instrument.classloading .SimpleLoadTimeWeaver" />
                      </property>
                      --></bean>

                      <bean id="dataSource"
                      class="org.springframework.jdbc.datasource.DriverM anagerDataSource">


                      <!-- <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/> -->

                      <!-- <property name="url" value="jdbc:db2://192.168.23.36:50000/dwspring"/>
                      <property name="url" value="jdbc:hsqldb:hsql://localhost/dwspring" /> -->

                      <property name="driverClassName" value="org.postgresql.Driver" />
                      <property name="url" value="jdbcostgresql://localhost:5432/postgres" />
                      <property name="username" value="postgres" />
                      <property name="password" value="admin" />

                      </bean>

                      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
                      <property name="entityManagerFactory" ref="entityManagerFactory" />
                      <property name="dataSource" ref="dataSource" />
                      </bean>
                      <bean id="employeeDAO" class="com.ibm.dw.spring2.EmployeeDAO">
                      <property name="entityManagerFactory" ref="entityManagerFactory" />
                      </bean>
                      </beans>


                      please help me out to solve it.

                      Comment

                      Working...
                      X