At first I did not mention what was the key component of this problem: here I am using TestNG.
I have a persistence DAO layer. It works great as part of my small web application (I have a classic controller, service, DAO design). I can update this question along with my XML documents.
My service level
@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; }
My DAO layer
@Repository public class UserDaoImplHibernate implements UserDao { @Autowired private SessionFactory sessionFactory;
And here is my test class
@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);
But for my test class, Autowiring userDao always returns Null , I'm just starting to do tests in Spring, and I'm a bit lost. Any pointers are welcome.
Last edited by Boris Treukhov
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);
And this is my 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" > <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>
I did not add repository-config.xml as this is enough to access userDao . I still get userDao equal to zero though.
Thank you in advance
source share