spring中测试用的新写法 Junit4.8 + springtest.jar

引用springtest  jar包和其他相关jar包  以及junit4

首先写一个基类 引用AbstractTransactionalJUnit4SpringContextTests

如下

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class BaseTestCase extends AbstractTransactionalJUnit4SpringContextTests{

}

写一个测试类  继承基类 然后加载自己需要的spring.xml文件

测试方法要+ @Test 方法名要 名字+Test

 @Resource注入dao接口
@ContextConfiguration(locations = "classpath:config/spring/test.xml")
public class DaoTest extends BaseTestCase{
 @Resource
 private TestDao testDao;
 @Test
 public void allTest(){
  testDao.dotest();
 }
}

猜你喜欢

转载自zwxiaole.iteye.com/blog/1744176