Results 1 to 5 of 5

Thread: junit 4 and dependency injection

  1. #1

    Default junit 4 and dependency injection

    I'd love to post a question on the above, but keep getting this nonsensical error:

    1. You are only allowed to post URLs to other sites after you have made 1 post or more

  2. #2

    Default

    let's try now....


    I've read the documentation, searched the forums, pulled my hair out, run out of ideas and am now hoping someone can point out what I'm missing.

    I have a test where I inject the SessionFactory and run a test using @Test. The problem is that when the test runs the SessionFactory is null. When I run in the debugger I can see that the setSessionFactory is run and the variables are set correctly. When I enter the test the variables are null.

    I'm using junit 4.5, spring framework 3.0.M1, hibernate 3.2.6 within eclipse 3.4.1

    The code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"../../../applicationContext-datasource.xml"})
    @TestExecutionListeners({DependencyInjectionTestEx ecutionListener.class})
    public class ReportingAccessDaoTest {

    private SessionFactory sessionFactoryCore;
    private Session session;

    @Autowired
    public void setSessionFactoryCore(SessionFactory sessionFactoryCore) {
    this.sessionFactoryCore = sessionFactoryCore;
    this.session = this.sessionFactoryCore.openSession();
    Assert.assertNotNull(this.session);
    }

    @Test
    public void one() {
    this.reportingAccessDao.setSession(this.session);
    Assert.assertNotNull(this.session);
    ReportingAccess reportingAccess = this.reportingAccessDao.getReportingAccessByUserId (4);
    Assert.assertNotNull(reportingAccess);
    }

    }
    </pre>[/html]

    The application context XML:
    HTML Code:
    <pre>
        <bean id="dataSourceCore" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
                     .... removed for readabillity......
        </bean>
        <bean id="sessionFactoryCore" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSourceCore"/>
                     .... removed for readabillity......
        </bean>
                
        <!-- DAOs -->
        <bean id="reportingAccessDaoTest" class="com.enservio.dao.ReportingAccessDaoTest">
            <property name="reportingAccessDao" ref="reportingAccessDao"/>
            <property name="sessionFactoryCore" ref="sessionFactoryCore"/>
        </bean>
                
    </beans>
    
    </pre>

  3. #3
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    I don't think this is your problem, but look at section 8.3.7.5.4 of the pdf of the spring reference guide.

    I had a problem where I wasn't extending AbstractTransactionalTestNGSpringContextTests but was (wrongly) extending AbstractTestNGSpringContextTests. But I think I was getting an error about the session being closed.

    My class is
    Code:
    @Test(groups = { "db", "dao" })
    @ContextConfiguration(locations = { "classpath:test-context.xml",
    		"FacilsDowaitlistDaoTest-context.xml" })
    public final class FacilsDowaitlistDaoTest extends
    		AbstractTransactionalTestNGSpringContextTests {
    	private final transient Logger log = LoggerFactory.getLogger(getClass());
    
    	@Autowired
    	private FacilsDowaitlistDao facilsDowaitlistDao;
    
    	/**
         */
    	@Test(groups = { "facilsDowaitlist" })
    	public void testGetFacilsDowaitlists() {
    		final List<Facility> facilsDowaitlist = facilsDowaitlistDao
    				.getFacilsDowaitlist();
    
    		for (final Facility eachFacilsDowaitlist : facilsDowaitlist) {
    			log.debug("fullName: {}", eachFacilsDowaitlist.getFullname());
    		}
    	}
    }
    FacilsDowaitlistDaoTest-context.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans:beans
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
        <beans:bean
             id="facilsDowaitlistDao"
             class="edu.berkeley.ist.waitlist.db.hbm.dao.FacilsDowaitlistDao">
        </beans:bean>
    </beans:beans>
    test-context.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans:beans
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <!--
    wait list
    -->
    
        <!-- Activates annotation-based bean configuration -->
        <context:annotation-config />
    
        <!-- Scans for application @Components to deploy -->
        <context:component-scan
            base-package="edu.berkeley.ist.waitlist"
        />
    
        <tx:annotation-driven
            transaction-manager="transactionManager"
        />
    
        <beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <beans:property
                name="sessionFactory"
                ref="sessionFactoryBean"
            />
        </beans:bean>
    
        <beans:bean
            id="hibernateTemplate"
            class="org.springframework.orm.hibernate3.HibernateTemplate"
            p:sessionFactory-ref="sessionFactoryBean"
        />
    
        <beans:bean id="sessionFactoryBean"
                class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <beans:property
                name="dataSource"
                ref="dataSource"
            />
    
            <beans:property
                name="packagesToScan"
                value="edu.berkeley.ist.waitlist.db.hbm.dto"
            />
    
            <beans:property name="hibernateProperties">
                <beans:props>
                    <beans:prop key="hibernate.dialect">${hibernate.dialect}</beans:prop>
                    <beans:prop key="hibernate.default_schema">${hibernate.default_schema}</beans:prop>
    <!--                <beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>-->
    <!--                <beans:prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</beans:prop>-->
                    <beans:prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</beans:prop>
                </beans:props>
            </beans:property>
        </beans:bean>
    
        <!-- c3p0 pooling data source -->
        <beans:bean
            id="dataSource"
            class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close"
            p:driverClass="${jdbc-db2.driverClass}"
            p:jdbcUrl="${db2.url}"
            p:user="${db2.user}"
            p:password="${db2.password}"
            p:maxStatements="180"
            p:idleConnectionTestPeriod="60"
            p:testConnectionOnCheckin="true"
        />
    </beans:beans>

  4. #4
    Join Date
    Nov 2006
    Posts
    3

    Default

    sarahandartemis, did you solve your problem? I'm stuck with the same issue.

  5. #5
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    Hi sarahandartemis,

    There are several things wrong both with your configuration and your test code. First and foremost, I recommend you take a good look at the PetClinic sample application delivered with the Spring Framework. In there you will see several working transactional integration tests (e.g., AbstractClinicTests, HibernateClinicTests, etc.). Second, I recommend that you re-read the corresponding sections of the Spring Reference Manual on the TestContext framework, especially the section on transactions.

    Let me give you a few pointers as well:

    1. For a transactional integration test, do not configure only the DependencyInjectionTestExecutionListener. You need to configure the TransactionalTestExecutionListener as well. My advice: completely delete your @TestExecutionListeners configuration. You don't need it, since the defaults will suffice for you.
    2. On your Hibernate SessionFactory, call getCurrentSession(), not openSession().
    3. Never configure your test class as a bean in the Spring ApplicationContext. DependencyInjectionTestExecutionListener takes care of that for you. So delete your reportingAccessDaoTest bean declaration.


    Good luck,

    Sam

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •