junit适配器模式应用
适配器模式
定义:
将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作;
构成:
目标抽象角色(Target):定义客户要用的特定领域的接口
适配器(Adapter):调用另一个接口,作为一个转换器
适配器(Adaptee):定义一个接口,Adapter需要接入
比如我们家的台灯的插头是2相的,但是我们墙壁上的电源插座是3相的,那我们如何用呢???可以去买一个插排,插排接电源的是3孔的,插排上有很多插孔是2孔的,那么插排起到的作用就是桥梁,起转换的作用,在这里就是适配器模式中的适配器
分类:
常用的适配器模式有两种:类适配器(采用继承的方式)和对象适配器(采用对象组合的方式)
java代码实现:
类适配器:
//台灯插头
public interface Target {
public void method1();
}
//墙壁上的电源
public class Adaptee {
public void method2(){
System.out.println("Adaptee.method2()");
}
}
//插排
public class Adapter extends Adaptee implements Target {
@Override
public void method1() {
this.method2();
}
}
public class Client {
public static void main(String[] args) {
Target target = new Adapter();
target.method1();
}
}
对象适配器:
//台灯插头
public interface Target {
public void method1();
}
//墙壁上的电源
public class Adaptee {
public void method2(){
System.out.println("Adaptee.method2()");
}
}
//插排
public class Adapter implements Target {
private Adaptee adaptee; public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
} @Override
public void method1() {
adaptee.method2();
}
}
public class Client {
public static void main(String[] args) {
Target target = new Adapter(new Adaptee());
target.method1();
}
}
适配器模式在junit3框架中的应用
TestCase.java中的runTest()方法
public void runBare() throws Throwable {
setUp();
try {
runTest();
}
finally {
tearDown();
}
}
protected void runTest() throws Throwable {
assertNotNull(fName);
Method runMethod= null;
try {
runMethod= getClass().getMethod(fName, null);
} catch (NoSuchMethodException e) {
fail("Method \""+fName+"\" not found");
}
if (!Modifier.isPublic(runMethod.getModifiers())) {
fail("Method \""+fName+"\" should be public");
}
runMethod.invoke(this, new Class[0]);
}
在runBare()方法中,通过runTest()方法将我们自己编写的testXXX()方法进行了适配,使得junit框架可以执行我们自己编写的TestCase;
runTest()方法中,首先获得我们自己编写的testXXX方法所对应的Method对象(无参),然后检查该Method对象所编写的方法是否是pulbic的,如果是则调用Method对象的invoke方法来执行我们自己编写的testXXX方法。
在这里目标接口Target和适配器Adapter变成了同一个类TestCase,而测试用例,作为Adaptee。
junit3中引入适配器模式的好处:
1)使用Adapter模式简化测试用例的开发,通过按照方法命名的规范来开发测试用例,不需要进行大量的类继承,提高代码的复用,减轻测试人员的工作量;
2)使用Adapter可以重新定义Adaptee的部分行为,如增强异常处理等
junit适配器模式应用的更多相关文章
- junit设计模式--适配器模式
适配器(Adapter)模式 在软件系统中,由于环境的变化,常常需要将"一些现存的对象"放在新的环境中应用,但是新环境要求的接口是这些现存对象所不满足的.那么如何应对这种" ...
- junit测试延伸--方法的重复测试
在实际编码测试中,我们有的时候需要对一个方法进行多次测试,那么怎么办呢?这个问题和测试套件解决的方案一样,我们总不能不停的去右键run as,那怎么办呢?还好伟大的junit帮我们想到了. OK,现在 ...
- 设计模式课程 设计模式精讲 12-2 适配器模式coding
1 重要 1.1 类适配器和对象适配器最大的区别 2 代码演练 2.1 代码演练1(类适配器模式) 2.2 代码演练2(对象适配模式) 2.3 代码演练3(具体应用场景) 1 重要 1.1 类适配器和 ...
- 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file
我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- PHP设计模式(七)适配器模式(Adapter For PHP)
适配器模式:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 如下图(借图): // 设置书的接口 // 书接口 interface BookI ...
- AndroidStudio — Error:Failed to resolve: junit:junit:4.12错误解决
原博客:http://blog.csdn.net/u013443865/article/details/50243193 最近使用AndroidStudio出现以下问题: 解决:打开app下的buil ...
- 「译」JUnit 5 系列:环境搭建
原文地址:http://blog.codefx.org/libraries/junit-5-setup/ 原文日期:15, Feb, 2016 译文首发:Linesh 的博客:环境搭建 我的 Gith ...
- 《JS设计模式笔记》 5,适配器模式
//适配器模式的作用就像一个转接口. jQuery("#"+id); $id=function (id) { return jQuery("#"+id)[0]; ...
随机推荐
- js页面取值的三种方式
<input id=""<radio <checkbox<div<img对于这些标签内参数取值,一般分为三种类型:一.有关id取值用 #:取id处的v ...
- (转载)postgresql navicat 客户端连接验证失败解决方法:password authentication failed for user
命令:su - postgres CREATE USER foo WITH PASSWORD 'secret'; ==================== 1.2个配置修改 postgresql.co ...
- C# & SQL Server大数据量插入方式对比
以下内容大部分来自: http://blog.csdn.net/tjvictor/article/details/4360030 部分内容出自互联网,实验结果为亲测. 最近自己开发一个向数据库中插入大 ...
- 怎样减少FLASH影片文件过大——绝对好用
网站建设中怎样减少FLASH影片文件过大 一,制作前的处理 1声音(mp3): GoldWave中打开需要处理的mp3,然后把它另存为---在最下一栏的属性中选择较低的字节数,例如,本来的mp3 ...
- Spring MVC 教程,快速入门,深入分析
http://elf8848.iteye.com/blog/875830/ Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门 资源下载: ...
- 95. Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- C#之线程和并发
建议大家对C#撑握的不错的时候,可以去看一些开源项目.走技术这条路,就要耐得住寂寞(群里双休日说要让群主找妹子进群的人必须反思),练好内功.不撑握C#高级知识点,别想看懂优秀的开源项目,更别指望吸收其 ...
- 1.运行Android Studio,一直提示:Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
1.解决问题办法:菜单栏,Tools -> Adnroid -> enable ADB integration勾上 2.暂时性的解决方案:在Android Studio中的:Prefere ...
- C++14使用std::integer_sequence展开tuple作为函数的参数
元组是一种长度固定的允许有不同类型元素的集合,根据元素的个数不同又分别称作一元组.二元组.三元组等.C++11中标准库增加了一个叫std::tuple的类模板,用于表示元组. 下面的代码演示了使用C+ ...
- phpStudy Linux安装集成环境 (CentOS--7)
phpStudy for Linux (lnmp+lamp一键安装包) phpStudy for Linux 支持Apache/Nginx/Tengine/Lighttpd, 支持php5.2/5.3 ...