在使用JUnit进行单元测试期间,Spring自动装配仅适用于以下文件:
src/test/org/baudo
但不是文件:
src/main/org/baudo
因此,当我测试自动装配sessionFactory对象的DAO时,结果是我的会话工厂为null。然后我得到以下堆栈跟踪:
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)
这是我扫描组件的配置:
<context:component-scan base-package="org.baudo" />
事实是我有两个具有相同名称的包(一个用于测试,一个用于主要)。
我可以做些什么来扫描所有文件以进行自动装配(而不仅仅是在测试目录下)?
更多信息:
我正在使用Spring MVC和Hibernate与Maven
这是我的测试DAO:
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations={"/spring-servlet.xml"})
public class MyDaoTest {
@Autowired
private SessionFactory sessionFactory; // This contains a value
// Other stuff here
}
这是我的DAO:
public class MyDao {
@Autowired
private SessionFactory sessionFactory; // This is null
// Other stuff here
}
My Dao在:
MyProject\src\main\java\org\baudo\dao
我的测试在:
MyProject\src\test\java\org\baudo\dao
我的spring-servlet.xml也包含
<context:annotation-config />
<tx:annotation-driven />
这是我定义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>
我应该添加其他内容以使其正常工作吗?
答案 0 :(得分:1)
最可能的原因是您是直接创建MyDao对象而不是通过Spring BeanFactory
创建。您必须将要测试的DAO自动装配到测试类中:
// Here live the annotations ...
public class MyDaoTest {
@Autowired
private MyDao myDao;
@Test
public void testSomething() {
// Do what you need with myDao
}
}
或使用AspectJ编译器/ weaver并要求spring“建议”new
操作
<context:spring-configured />