0

During unit tests with JUnit, Spring autowiring is only working for files in:

src/test/org/baudo

But not files in:

src/main/org/baudo

So when I test my DAO that autowires a sessionFactory object, the result is my session factory is null. Then I get the following stack trace:

java.lang.NullPointerException
at org.baudo.dao.MyDAO.addPerson(MyDao.java:46)
at org.baudo.dao.MyDaoTest.testAddPerson(MyDaoTest.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

This is my configuration for scanning components:

 <context:component-scan base-package="org.baudo" />

The fact is that I have two packages with the same name (one for testing and one under main).

What can I do in order to scan all the files for autowiring (and not only under test dir)?

Further information:

I'm using Spring MVC and Hibernate with Maven

This is my test DAO:

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations={"/spring-servlet.xml"})
public class MyDaoTest {

    @Autowired
    private SessionFactory sessionFactory; // This contains a value

    // Other stuff here
}

This is my DAO:

public class MyDao {

    @Autowired
    private SessionFactory sessionFactory; // This is null

    // Other stuff here
}

My Dao is under:

 MyProject\src\main\java\org\baudo\dao

My test is under:

 MyProject\src\test\java\org\baudo\dao

My spring-servlet.xml also contains

<context:annotation-config />

<tx:annotation-driven />

This is the way I defined my bean:

<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>

Should I add something else to make it working?

4

1 回答 1

1

The most likely reason is that you are creating the MyDao object directly and not via Spring BeanFactory. You have to either autowire the DAO you want to test into your test class:

// Here live the annotations ... 
public class MyDaoTest {
    @Autowired
    private MyDao myDao;

    @Test
    public void testSomething() {
        // Do what you need with myDao
    }
}

or use AspectJ compiler/weaver and ask spring to "advise" the new operation

<context:spring-configured />
于 2013-07-13T10:44:46.107 回答