junit4进行单元测试时,报错appliactionContext.xml无法加载,在网上找了很多方法都没法解决(一个是看classpath里spring位置,一个是windows属性里先把编译里的一条去掉,保存,在加上)。
后来,问前辈发现问题,有数据库情况,要加入如下代码:
@Override @Resource(name = "dataSource") public void setDataSource(DataSource dataSource) { super.setDataSource(dataSource); }
具体,如下
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml","classpath:spring-mvc.xml","classpath:applicationContext-security.xml"}) @TransactionConfiguration(transactionManager="txManager",defaultRollback=false) public class testInitSystemData extends AbstractTransactionalJUnit4SpringContextTests{ @Override @Resource(name = "dataSource") public void setDataSource(DataSource dataSource) { super.setDataSource(dataSource); } @Resource(name="baseService") private IBaseService baseService; @Test public void initPermission() throws Exception{ List<Permission> list = new ArrayList<Permission>();