我測試下面的DAO使用JUnit自動裝配:春天沒有單元測試使用JUnit
@Repository
public class MyDao {
@Autowired
private SessionFactory sessionFactory;
// Other stuff here
}
正如你所看到的,SessionFactory則是使用Spring自動裝配。當我運行測試時,sessionFactory保持爲空,我得到一個空指針異常。
這是春天SessionFactory的配置:
<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>
有什麼不對?我怎樣才能爲單元測試啓用自動裝配?
更新:我不知道它是否是運行JUnit測試的唯一方法,但請注意,我正在Eclipse中運行它,右鍵單擊測試文件並選擇「run as」 - >「JUnit test 「
是'包含在掃描路徑MyDao'? – Reimeus
是的,包括在內 – user1883212
' '呢?你有你的XML嗎? –