• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Spring MVC, Hibernate and JUnit: not autowiring problem

 
Greenhorn
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 )
@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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic