您的位置:首页 > 编程语言 > Java开发

Spring整合JUnit4测试

2016-05-27 14:53 441 查看
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext-dao.xml",
"classpath:spring/applicationContext-service.xml","classpath:spring/applicationContext-trans.xml"})
public class SpringJunitTest {
    //Spring运行器+Spring配置文件,我们就得到了Spring容器,容器拿到了,里面的东西(bean等)就拿到了
}


需要做单元测试时继承这个类

public class TestTestTb extends SpringJunitTest {

@Autowired
private TestTbService testTbService;

@Test
public void testAdd() throws Exception{
TestTb testTb = new TestTb();
testTb.setName("zhangsan");
testTbService.addTestTb(testTb);

}
}


提高了扩展性

注意,有多个配置文件时用{}

@ContextConfiguration(locations = {"classpath:spring/applicationContext-dao.xml",
"classpath:spring/applicationContext-service.xml","classpath:spring/applicationContext-trans.xml"})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: