![](/img/trans.png)
[英]TransactionManager and datasource in spring boot - spring data
[英]Spring jdbctemplate, datasource, transactionManager
為了測試,我有一個抽象類。
public abstract class BaseTestClass
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource)
{
this.setJdbcTemplate(new JdbcTemplate(dataSource));
}
...
}
@Transactional
@ContextConfiguration(locations = {"/spring/test/test-dao-context.xml"})
public class TestUser extends BaseTestClass{
...
}
在test-dao-context.xml文件中,我有事務管理器和數據源。
永遠不會調用SetDataSource,因此當我嘗試進行測試時,我得到一個null pointerException。
您需要使用適當的運行程序運行測試,該運行程序是用於Spring驅動的測試的SpringJUnit4ClassRunner
。 否則,任何地方都不會注入任何東西,因為沒有Spring Container管理所有實例。
有關詳細信息,請檢查彈簧單元測試 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.