@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"})