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"})
相关文章推荐
- java 序列化和反序列化
- struts2拦截器
- java.util.vector中的vector的详细用法及与list的区别
- Spring 的 init-method 和 destory-method
- JAVA压缩文件之-ZipOutputStream
- java中软引用与弱引用的笔记
- Java学习笔记之String类
- Spring+SpringMVC重复加载配置文件问题
- java设计模式之策略模式
- Struts2_表单重复提交问题
- java定时器无法自动注入的问题解析
- [置顶] Java中堆和栈的区别分析
- java.lang.UnsatisfiedLinkErro错误解决
- 《关于JAVA编译器JDK的安装》
- Java学习笔记之多态变量和向上造型
- org.springframework.beans.factory.NoSuchBeanDefinitionException/expected at least 1 bean which quali
- SpringMVC登录实例
- 单例 (JAVA)
- Java学习笔记之对象交互
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别