Frühling + TestNG-integration-tests, die Injektion von DAO mit Anmerkungen ausfällt

Ich zuerst nicht erwähnt habe, was war der Schlüssel-Komponente für dieses Problem: ich bin mit TestNG hier.

Ich habe eine DAO-Schicht performing Persistenz. Es funktioniert gut als Teil meiner kleinen web-app (ich habe einen classic-Controller, Service -, DAO-Layer design). Ich kann aktualisieren, die diese Frage mit meine XML-Dateien, falls erforderlich.

Mein Service layer

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public GoodVibeUserDetails getUser(String username) throws UsernameNotFoundException {

        GoodVibeUserDetails user = userDao.getDetailsRolesAndImagesForUser(username);

        return user;
    }

    //more methods...

}

Mein DAO-Schicht

@Repository
public class UserDaoImplHibernate implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    //My methods using sessionFactory & "talking" to the Db via the sessionFactory
}

Und hier ist meine Test-Klasse

@Component
public class UserDaoImplHibernateTests{

    @Autowired
    private UserDao userDao;

    private GoodVibeUserDetails user; 

    @BeforeMethod
    public void beforeEachMethod() throws ParseException{
        user = new GoodVibeUserDetails();
        user.setUsername("adrien");
        user.setActive(true);
        //& so on...
    }

    /*
     * When everything is fine - test cases
     */
    @Test
    public void shouldAcceptRegistrationAndReturnUserWithId() throws Exception{
        assertNotNull(userDao) ;
        user = userDao.registerUser(user);
        assertNotNull(user.getId()) ;
    }

    //more test cases...

}

Aber für meine test-Klasse das Autowiring, userDao gibt immer Null, ich bin nur beginnen tun tests in den Frühling und ich bin ein bisschen verloren. Alle Hinweise sind willkommen.


Neuesten edit nach Boris Treukhov Antwort

import ...
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class UserDaoImplHibernateTests{

    @Autowired
    @Qualifier("userDao")
    private UserDao userDao;

    private GoodVibeUserDetails user; 

    @BeforeMethod
    public void beforeEachMethod() throws ParseException{
        user = new GoodVibeUserDetails();
        user.setUsername("adrien");
        user.setActive(true);
        //& so on...
    }

    /*
     * When everything is fine - test cases
     */
    @Test
    public void shouldAcceptRegistrationAndReturnUserWithId() throws Exception{
        assertNotNull(userDao) ;
        user = userDao.registerUser(user);
        assertNotNull(user.getId()) ;
    }

    //more test methods...

}

Und das ist mein applicationContext.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:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" >


    <!-- the application context definition scans within the base package of the application -->
    <!-- for @Components, @Controller, @Service, @Configuration, etc. -->
    <context:annotation-config />
    <context:component-scan base-package="com.goodvibes" />

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
        p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}" 
        p:username="${jdbc.username}" p:password="${jdbc.password}" />


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">${jdbc.show_sql}</prop>
                <prop key="hibernate.connection.SetBigStringTryClob">true</prop>
                <prop key="hibernate.jdbc.batch_size">0</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    [...]

</beans>

Habe ich nicht hinzufügen repository-config.xml so das sollte genug sein, um den Zugang userDao. Ich bekomme immer noch userDao gleich null obwohl.

Vielen Dank im Voraus

InformationsquelleAutor Adrien Be | 2012-10-26

Schreibe einen Kommentar