• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Spring MVC, Hibernate and JUnit: not autowiring problem

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created the following test class in JUnit:

@RunWith( SpringJUnit4ClassRunner.class )
public class MyDaoTest {

private SessionFactory sessionFactory;

// Other stuff here


The application context is correctly loaded with the following configuration from the spring-context.xml:

<bean id="sessionFactory"
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<property name="configurationClass">
<property name="hibernateProperties">
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>

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:

public class MyDao {

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?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic