Spring重温(五)— — spring和junit整合
2018-03-21 14:15
267 查看
Spring和junit整合,没啥难度。无非就是导入架包,然后级本可以进行junit测试了。本着好记忆不如烂笔头的想法也将它记录下来!
方法一:(注解方式,手动来加载配置文件)
方法二:(junit的套件来加载)
结果都可以,但是相对来说,我们会比较喜欢第二种套件测试吧!
注意: 第一种方式的弊端: 一:每一次运行都需要初始化一次容器,而且代码都是硬编码,不利于修改
二:如果有数据库操作,每次都要看一下数据库是否有数据插入成功。按道理在自己写demo是可以的。但是如果数据量一旦起来,想想都觉得可怕。。
附:
修改于2018.3.21晚!
使用了
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-test2.xml"}) 这个注解@ContextConfiguration调用的是applicationContext的容器。但是,每次实例化一个bean都有走一次BeanPostProcessor接口的方法。
程序人生,与君共勉
架包:
junit-4.9.jar和spring-test-4.1.6.RELEASE.jar这两个jar包,版本可以自己选择,如果是要使用注解形式,junit版本尽量在4.9以上(记得不是很清楚了)。入门阶段用的jar如下:测试代码:
提供多种junit的方法。当复习一下junit吧!方法一:(注解方式,手动来加载配置文件)
package com.wen.test; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wen.test4.service.StudentService; public class SpringTest { //这里原本是要用ApplicationContext接口的,但是延续上一篇 //文章的bean生命周期的例子。需要手动关闭,所以这里有ClassPathXmlApplicationContext这个类 ClassPathXmlApplicationContext applicationContext; @Before public void setUp(){ //在这里做初始化 applicationContext= new ClassPathXmlApplicationContext("applicationContext-test2.xml"); } @Test public void testSpring(){ //接收bean并强转 StudentService studentService=(StudentService) applicationContext.getBean("studentService"); System.out.println("第九步:我们写的业务逻辑 start"); studentService.choiceCurriculum(); System.out.println("第九步:我们写的业务逻辑 end"); } @After public void teardown(){ //手动关闭 applicationContext.close(); } }测试结果:
方法二:(junit的套件来加载)
package com.wen.test; import javax.annotation.Resource; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.wen.test4.service.StudentService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext-test2.xml"}) public class SpringTest { //注入 @Resource StudentService studentService; @Test public void test(){ //开始测试 System.out.println("第九步:我们写的业务逻辑 start"); studentService.choiceCurriculum(); System.out.println("第九步:我们写的业务逻辑 end"); } }测试结果:(消息太多,分两次截图)
结果都可以,但是相对来说,我们会比较喜欢第二种套件测试吧!
注意: 第一种方式的弊端: 一:每一次运行都需要初始化一次容器,而且代码都是硬编码,不利于修改
二:如果有数据库操作,每次都要看一下数据库是否有数据插入成功。按道理在自己写demo是可以的。但是如果数据量一旦起来,想想都觉得可怕。。
总结:
第一种方式相对来说可以当学习用的,但是实践时没有必要了。第二种方式个人比较推荐!但是第二种方式小编没有理解为什么要执行2次BeanPostProcessor接口里的方法!希望有大神帮我解答一下!附:
修改于2018.3.21晚!
使用了
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-test2.xml"}) 这个注解@ContextConfiguration调用的是applicationContext的容器。但是,每次实例化一个bean都有走一次BeanPostProcessor接口的方法。
程序人生,与君共勉
相关文章推荐
- Spring整合Junit测试框架
- Spring Test 整合 JUnit 4 使用总结
- spring 整合junit进行测试
- Spring Test 整合 JUnit 4 使用总结
- Spring Test 整合 JUnit 4 使用总结
- Spring整合Junit例子
- Spring整合JUnit实现测试用例
- Spring的基础学习(十二)——Spring与Junit的整合
- Spring Test 整合 JUnit 4 使用总结
- Spring Test 整合 JUnit 4 使用
- Spring入门(11)-Spring与Junit整合
- Spring 整合 Junit4 进行单元测试
- Spring整合Junit框架进行单元测试Demo
- Spring系列之整合其他框架(一):Spring Test+JUnit整合使用
- Spring 03 整合Junit+Web+SSH
- Spring学习笔记——整合junit
- Spring整合JUnit框架进行单元测试代码使用详解
- JUnit与Spring的整合——JUnit的TestCase如何自动注入Spring容器托管的对象
- Spring Test 整合 JUnit 4 使用总结
- Spring Test 整合 JUnit 4 使用总结