I created the following
test class in
JUnit:
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations={"/spring-servlet.xml"})
public class MyDaoTest {
@Autowired
private SessionFactory sessionFactory;
// Other stuff here
}
The application context is correctly loaded with the following configuration from the spring-context.xml:
<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">true</prop>
</props>
</property>
</bean>
I also have these lines in my configuration:
<context:annotation-config />
<context:component-scan base-package="org.baudo" />
<tx:annotation-driven />
All my files are into the namespace org.baudo
The fact is that the sessionFactory inside my Dao is not autowired. This cause me a null pointer exception:
@Repository
public class MyDao {
@Autowired
private SessionFactory sessionFactory;
// Some stuff here
}
Important update: I found that autowiring work if I use it in a test file, but not a file under main:
src/main/org/baudo/MyFile.java HERE it doesn't work
src/test/org/baudo/MyFile.java HERE it works
What may be the problem?