JUnit4的简单使用
来源:互联网 发布:解绑支付宝与淘宝账户 编辑:程序博客网 时间:2024/06/05 02:46
之前做Android开发,因为涉及到UI和Android本身的Activity的生命周期影响,项目的测试全是整个项目一起跑起来测试,所以很少写测试类。渐渐的快忘了Junit这个东西的存在。
最近在学习Spring框架使用,决定把Junit4这个单元测试工具用起来。记录下使用方法。(只代表个人使用习惯)
略过集成Junit4步骤。
以测试Spring框架为例
创建一个BaseJUnit4Test类
import org.junit.After;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.BlockJUnit4ClassRunner;/** * date : 2017/11/15 17:24 * author : zhengliang */@RunWith(BlockJUnit4ClassRunner.class)public abstract class BaseJUnit4Test { public BaseJUnit4Test() { } /** * Test执行之前调用,可以做一些初始化操作。 */ @Before public void before(){ ... } /** * Test执行完成后调用,可以做一些回收和销毁操作。 */ @After public void after(){ ... } /** * 具体的测试方法 */ @Test public abstract void test();}
具体使用流程
import org.junit.After;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.BlockJUnit4ClassRunner;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.util.StringUtils;/** * date : 2017/11/15 17:24 * author : zhengliang */@RunWith(BlockJUnit4ClassRunner.class)public abstract class BaseJUnit4Test { /** * Spring容器上下文 */ private ClassPathXmlApplicationContext context; /** * Spring配置文件路径 */ private String springXmlPath; public BaseJUnit4Test() { } public BaseJUnit4Test(String springXmlPath) { this.springXmlPath = springXmlPath; } @Before public void before(){ //如果地址为空 设置默认值 if (StringUtils.isEmpty(springXmlPath)) { springXmlPath = "classpath*:spring-*.xml"; } context = new ClassPathXmlApplicationContext(springXmlPath); context.start(); } @After public void after(){ context.destroy(); } @Test public abstract void test(); protected <T extends Object> T getBean(String beanId){ return (T) context.getBean(beanId); } protected <T extends Object> T getbean(Class<T> clazz){ return context.getBean(clazz); }}
实现的测试类
import com.sie.bunny.base.BaseJUnit4Test;/** * date : 2017/11/16 11:39 * author : zhengliang */public class TextEditorTest extends BaseJUnit4Test { public TextEditorTest (){ super("classpath*:spring-dependency-injection.xml"); } public void test() { TextEditor textEditor = super.getBean("textEditor"); textEditor.spellChecker(); }}
阅读全文
0 0
- JUnit4的简单使用
- junit4简单使用
- Junit4的简单使用说明
- JUnit4的使用
- JUnit4的使用
- JUnit4的使用
- Junit4的使用
- Junit4--hamcrest的使用
- junit4的使用
- Junit4 -- TestSuite 的使用
- JUnit4笔记(一)---概述和简单使用
- Junit4 简单使用及示例代码
- Junit4的使用及实例
- JUnit4的使用和配置
- [1]Junit4-Assertions的使用
- Junit4测试套件的使用
- junit4单元测试框架的使用
- Junit4-使用JUnit4
- Java(28):基础语法(2):修饰符的理解和使用
- MYSQL目录文件迁移
- Quartz-DateBuilder解读
- c++ primer 笔记,第十一章(关联容器)
- Low Level CAN Framework Application Programmers Interface
- JUnit4的简单使用
- VS2017初用体验
- vim 寄存器复制
- QScrollBar样式表总结
- 分布式锁-基于数据库实现
- No module named win32api问题解决
- Java(29):基础:JVM的理解和使用(收集自网上资源)
- 企业员工考勤管理子系统
- Git Pull Failed: Could not read from remote repository