This week's giveaway is in the Java in General forum.
We're giving away four copies of Java Challengers and have Rafael del Nero on-line!
See this thread for details.
Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

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/ HERE it doesn't work
src/test/org/baudo/ HERE it works

What may be the problem?
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic