Junit4测试框架与Spring集成测试

配置测试框架的测试基类,防止多次初始化Spring容器,测试类只需要继承基类即可

//BaseTest.class
@RunWith(SpringJUnit4ClassRunner.class)//使用junit4驱动
//引入配置文件
@ContextConfiguration(value = {"classpath:SpringConfig.xml", "classpath:Spring-MVC.xml", "classpath:MyBatis.xml"})
@Transactional(transactionManager = "dataSourceTransactionManager")//配置事务管理器
@Rollback(value = true)//值为true测试后该操作会回滚(不影响数据库),值为false则与此相反
public class BaseTest {
@Rule//junit测试方法规则
public TestWatcher testWatcher = new TestWatcher() {

@Override
protected void starting(Description description) {//开始执行时进行
// System.out.println(description.getMethodName()+" Starting.......");//方法名称
System.out.println(">>>>>>>>>>> " + description.getDisplayName() + " Starting.......");//全类名
}

@Override
protected void succeeded(Description description) {//执行成功进行
System.out.println(">>>>>>>>>>> " + description.getDisplayName() + " Succeeded.......");//全类名
}

@Override
protected void failed(Throwable e, Description description) {
System.out.println(">>>>>>>>>>> " + description.getDisplayName() + " >>>>>> Failed <<<<<<");//全类名
}
};
}