Announcement Announcement Module
Collapse
No announcement yet.
JUnit with Spring Roo Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JUnit with Spring Roo

    I developed a web application using spring roo. Now I have to test a class inside my web app and i want to use junit. How is it possible to configure it? This is my test class definition :

    @ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml"})
    public class TestScheduler extends AbstractJUnit4SpringContextTests


    but when i start my test i obtain this error :

    ERROR org.springframework.test.context.TestContextManage r - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener@1faac07d] to prepare test instance [test.it.consoft.passmobility.TestScheduler@3435ec9]
    java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getAp plicationContext(TestContext.java:157)
    at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.injectDependencies (DependencyInjectionTestExecutionListener.java:109 )
    at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.prepareTestInstanc e(DependencyInjectionTestExecutionListener.java:75 )
    at org.springframework.test.context.TestContextManage r.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.createTest(SpringJUnit4ClassRunner.j ava:211)



    Could you please help me?

    Thanks

    Simone

  • #2
    Hi,

    here is mine

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext*.xml")
    @Transactional
    @Configurable
    public class AccountTest {
    
    	/////////////////////////////////////////////////////	
    	//	TESTS
    	/////////////////////////////////////////////////////
    	
    	@Ignore
    	@Test
    	public void testGetAccount() {
    	}
    }

    Comment


    • #3
      Thank you Valerian for your response.
      I tried your configuration, but if i delete the @ignore i obtain the following exception :

      ERROR org.springframework.test.context.TestContextManage r - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener@66a33414] to prepare test instance [test.it.consoft.passmobility.TestScheduler@64b041b 0]
      java.lang.IllegalStateException: Failed to load ApplicationContext
      .....
      .....
      .....
      Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'transactionManager': Invocation of init method failed; nested exception is java.lang.IllegalStateException: No JTA UserTransaction available - specify either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName'


      Inside my applicationContext.xml i defined a transactionManager as follows :

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


      Probably I have to configure this inside my test class but i don't know how...

      Simone

      Comment


      • #4
        Hi,

        please post your applicationContext.xml and persistence.xml files. Are you able to run application with this context? If I use JTA type my appContext looks like this
        Code:
        <?xml version="1.0" encoding="UTF-8" standalone="no"?>
        <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
        
        <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
        <context:spring-configured/>
        <context:component-scan base-package="com.foo.bar">
                <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
                <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
        </context:component-scan>
        <tx:jta-transaction-manager userTransactionName="java:comp/UserTransaction"/>
            <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
            <jee:jndi-lookup  id="entityManagerFactory" jndi-name="SB3_DEV01" expected-type="javax.persistence.EntityManagerFactory"/>
        </beans>
        So I think that <tx:jta-transaction-manager /> is not valid. You can check it against xsd.

        Comment


        • #5
          I tried what you suggested but i obtained the same error.
          This my persistence.xml :

          <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
          <persistence-unit name="persistenceUnit" transaction-type="JTA">
          <provider>org.hibernate.ejb.HibernatePersistence </provider>
          <jta-data-source>java:/SodexoDS</jta-data-source>
          <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
          <property name="hibernate.hbm2ddl.auto" value="update"/>
          <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
          <property name="hibernate.connection.charSet" value="UTF-8"/>
          <property name="jboss.entity.manager.factory.jndi.name" value="it/consoft/MyEntityManagerFactory"/>
          <property name="hibernate.id.new_generator_mappings" value="false" />
          </properties>
          </persistence-unit>
          </persistence>


          and this is the applicationContext :

          <?xml version="1.0" encoding="UTF-8" standalone="no"?>
          <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schem...ng-jee-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

          <contextroperty-placeholder location="classpath*:META-INF/spring/*.properties"/>
          <context:spring-configured/>
          <context:component-scan base-package="it.consoft.passmobility">
          <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
          <context:exclude-filter expression="org.springframework.stereotype.Control ler" type="annotation"/>
          </context:component-scan>

          <tx:jta-transaction-manager userTransactionName="java:comp/UserTransaction"/>
          <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

          <bean class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean" id="entityManagerFactory">
          <property name="persistenceUnitName" value="persistenceUnit"/>
          <property name="dataSource" ref="dataSource"/>
          </bean>
          <jee:jndi-lookup id="entityManagerFactory"
          jndi-name="it/consoft/MyEntityManagerFactory"
          expected-type="javax.persistence.EntityManagerFactory" />

          <!-- dataSource bean -->
          <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
          <property name="driverClassName" value="${database.driverClassName}" />
          <property name="url" value="${database.url}" />
          <property name="username" value="${database.username}" />
          <property name="password" value="${database.password}" />
          </bean>

          <!-- ***************************************** -->
          <!-- Bean per il caricamento di properties -->
          <!-- ***************************************** -->
          <bean id="propertyLoader" init-method="init" class="it.consoft.passmobility.util.PropertyLoader "/>

          <!-- ***************************************** -->
          <!-- Bean per la parte di Scheduling/Scadenze -->
          <!-- ***************************************** -->
          <bean id="Scheduler" class="it.consoft.passmobility.scheduling.Schedule r" />

          <!-- ####### JOB ####### -->
          <bean id="JobScadenzaOrdine" class="org.springframework.scheduling.quartz.Metho dInvokingJobDetailFactoryBean">
          <property name="targetObject" ref="Scheduler"/>
          <property name="targetMethod" value="checkScadenzaOrdine" />
          </bean>

          <bean id="JobCreazioneOrdine" class="org.springframework.scheduling.quartz.Metho dInvokingJobDetailFactoryBean" >
          <property name="targetObject" ref="Scheduler"/>
          <property name="targetMethod" value="creaOrdine" />
          </bean>

          <bean id="JobGetFileFromCRM" class="org.springframework.scheduling.quartz.Metho dInvokingJobDetailFactoryBean" >
          <property name="targetObject" ref="Scheduler"/>
          <property name="targetMethod" value="getFileFromCrm" />
          </bean>

          <!-- ####### TRIGGER ####### -->
          <bean id="TriggerScadenzaOrdine" init-method="init" class="it.consoft.passmobility.scheduling.SimpleTr igger">
          <property name="schedulerName" value="JobScadenzaOrdine" />
          <property name="jobDetail" ref="JobScadenzaOrdine" />
          </bean>

          <bean id="TriggerCreazioneOrdine" init-method="init" class="it.consoft.passmobility.scheduling.CronTrig ger">
          <property name="schedulerName" value="JobCreazioneOrdine" />
          <property name="jobDetail" ref="JobCreazioneOrdine" />
          </bean>

          <bean id="TriggerGetFileFromCRM" init-method="init" class="it.consoft.passmobility.scheduling.CronTrig ger">
          <property name="schedulerName" value="JobGetFileFromCRM" />
          <property name="jobDetail" ref="JobGetFileFromCRM" />
          </bean>

          <!-- ####### FACTORY ####### -->
          <bean class="org.springframework.scheduling.quartz.Sched ulerFactoryBean">
          <property name="triggers">
          <list>
          <!--
          <ref local="TriggerScadenzaOrdine" />
          <ref local="TriggerCreazioneOrdine" />
          <ref local="TriggerGetFileFromCRM" />
          -->
          </list>
          </property>
          </bean>

          </beans>


          My web app works so i think it's a problem about junit configuration on my test class.
          Thanks again

          Simone

          Comment

          Working...
          X