Java的类继承机制 -- 应用于接口系统测试

首先,我们需要正确解析Java的类继承机制,通过一个demo即可。

例:

    学生信息处理

    基类:学生,完成所有学生的通用的方法

    职务类:担任了各种职务的学生,他们也是学生,但是和一般的学生不同,他们有别于普通学生,他们有额外的特征(字段)和技能(职务)


实现:


文件1:Student.java

package com.dufe.startup;

/**
 * @author administrator
 *
 */

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

/** 
 * 学生信息类 
 * 调用示例: 
 * 
 * Student student1 = new Student("wang", 1, 20, "BeiJing"); 
 * System.out.println(student1);  
 */ 
public class Student {

	static long mvId = 1;		// 从1开始,下面的方法使用此字段时,先调用,后递增
	
	String mstrName = null;
	String mstrId = null;
	String mstrAddress = null;
	int mvSex = 0;		// 0为男,1为女
	int mvAge = 0;
	
	String strIdMarkCode = null; 
	
	public Student() {
		this.mstrName = "noname";
		this.mstrId = getIdMark();
		this.mstrAddress = null;
		this.mvSex = 0;		// 0为男,1为女
		this.mvAge = 0;
	}
	
	public Student(String aName) {
		this.mstrName = aName;
		this.mstrId = getIdMark();
	}
	
	public Student(String aName, String aAddress) {
		this.mstrAddress = aAddress;
		this.mstrId = getIdMark();
	}
	
	public Student(String aName, int aSex, String aAddress) {
		this.mstrName = aName;
		this.mstrAddress = aAddress;
		this.mstrId = getIdMark();
	}
	
	public Student(String aName, int aSex, int aAge, String aAddress) {
		this.mstrName = aName;
		this.mstrAddress = aAddress;
		this.mvAge = aAge;
		this.mstrId = getIdMark();
	}
	
	protected String getIdMark() {
		GregorianCalendar calendar = (GregorianCalendar)(GregorianCalendar.getInstance());
		SimpleDateFormat DateFormatter = (SimpleDateFormat)SimpleDateFormat.getDateInstance();
		DateFormatter.applyPattern("yyyyMMdd");
		String strDatePrefix = DateFormatter.format(calendar.getTime()).toString();
		
		DecimalFormat decimalFormatter = (DecimalFormat)(NumberFormat.getNumberInstance());
		decimalFormatter.applyPattern("0000");
		String strNumberMarkCode = decimalFormatter.format(Student.mvId).toString();
		
		Student.mvId++;
		return strDatePrefix + strNumberMarkCode;
	}

	public String getName() {
		return mstrName;
	}

	public void setName(String aName) {
		this.mstrName = aName;
	}

	public String getId() {
		return String.valueOf(mvId);
	}

	public String getAddress() {
		return mstrAddress;
	}

	public void setAddress(String aAddress) {
		this.mstrAddress = aAddress;
	}

	public int getSex() {
		return mvSex;
	}

	public void setSex(int aSex) {
		this.mvSex = aSex;
	}

	public int getAge() {
		return mvAge;
	}

	public void setAge(int aAge) {
		this.mvAge = aAge;
	}
	
	public String toString() {
		StringBuffer information = new StringBuffer();
		
		if (this.mstrName == null || this.mstrId == null) {
			return null;
		}
		
		String separator = "\t";
		information.append(mstrName);
		information.append(separator);
		information.append(mstrId);
		information.append(separator);
		information.append(mvSex == 0 ? "男" : "女");
		information.append(separator);
		information.append(String.valueOf(mvAge));
		information.append(separator);
		information.append(mstrAddress);
		
		return information.toString();
	}
}


文件2:StudentManage.java

package com.dufe.startup;

/**
 * @author administrator
 *
 */
public class StudentManage extends Student {

	private String mstrDuty = null;
	
	/**
	 * 
	 */
	public StudentManage() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * Description: 仅指定名称的构造器
	 * @param aName 姓名
	 */
	public StudentManage(String aName) {
		super(aName);
		// TODO Auto-generated constructor stub
	}

	/**
	 * Description: 同时指定姓名和地址的构造器
	 * @param aName     	姓名
	 * @param aAddress		地址
	 */
	public StudentManage(String aName, String aAddress) {
		super(aName, aAddress);
		// TODO Auto-generated constructor stub
	}

	/**
	 * Description: 同时指定姓名、性别、地址的构造器
	 * @param aName		姓名
	 * @param aSex		性别,0为男,1为女
	 * @param aAddress	地址
	 */
	public StudentManage(String aName, int aSex, String aAddress) {
		super(aName, aSex, aAddress);
		// TODO Auto-generated constructor stub
	}

	/**
	 * Description: 同时指定姓名、性别、年龄、地址的构造器
	 * @param aName		姓名
	 * @param aSex		性别,0为男,1为女
	 * @param aAge		年龄,整数
	 * @param aAddress	地址
	 */
	public StudentManage(String aName, int aSex, int aAge, String aAddress) {
		super(aName, aSex, aAge, aAddress);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the mstrDuty
	 */
	public String getDuty() {
		return mstrDuty;
	}

	/**
	 * @param aDuty the mstrDuty to set
	 */
	public void setDuty(String sDuty) {
		this.mstrDuty = sDuty;
	}
	
	@Override
	public String toString() {
		StringBuffer information = new StringBuffer();
		
		if (this.mstrName == null || this.mstrId == null) {
			return null;
		}
		
		String separator = "\t";
		information.append(mstrName);
		information.append(separator);
		information.append(mstrId);
		information.append(separator);
		information.append(mvSex == 0 ? "男" : "女");
		information.append(separator);
		information.append(String.valueOf(mvAge));
		information.append(separator);
		information.append(mstrDuty);
		information.append(separator);
		information.append(mstrAddress);
		
		return information.toString();
	}
}

文件3:Startup.java

这个文件是整个包的调用(启动)入口,在这里就做单元测试了,如果要与他人协作,可以把这里的调用接口拿来使用

package com.dufe.startup;

/**
 * @author administrator
 *
 */


public class Startup {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student student1 = new Student("wang", 1, 20, "BeiJing");
		System.out.println(student1);
		
		Student student2 = new Student("zhang", 0, 22, "BeiJing");
		System.out.println(student2);
		
		Student student3 = new Student("zhao", 1, 23, "ShanDong");
		System.out.println(student3);
		
		Student student4 = new Student("sun", 0, 23, "SuChuan");
		System.out.println(student4);
		
		Student student5 = new Student("li", 0, 21, "JiangSu");
		System.out.println(student5);
		
		StudentManage studentManage1 = new StudentManage("zhou", 1, 21, "HeBei");
		studentManage1.setDuty("学生会主席");
		System.out.println(studentManage1);
		
		StudentManage studentManage2 = new StudentManage("qian", 1, 21, "NeiMengGu");
		studentManage2.setDuty("学生会副主席");
		System.out.println(studentManage2);
		
		StudentManage studentManage3 = new StudentManage("liang", 1, 21, "GuangDong");
		studentManage3.setDuty("文学会主席");
		System.out.println(studentManage3);
		
		StudentManage studentManage4 = new StudentManage("zhu", 1, 21, "ShangHai");
		studentManage4.setDuty("学生会文艺部长");
		System.out.println(studentManage4);
		
		StudentManage studentManage5 = new StudentManage("zou", 1, 21, "HeNan");
		studentManage5.setDuty("学生会联络部长");
		System.out.println(studentManage5);
		
		Student student6 = new Student("luo", 0, 21, "JiangSu");
		System.out.println(student6);
	}

}


运行结果如下图所示:



这个模型已经调通了,被我用在了下面的实例中:

app需要做接口测试,对于每一个接口的实现和数据的处理以及返回结果的正确性都要进行校验。

设计实现:

    定义一个接口类基类:Interface,然后实现它的基本字段和方法,字段主要是domain、port、parameters、deviceid、timestamp、token、rand、ver、platform、refer等,方法主要有send、receive等。

    分别定义不同的接口的类,继承于Interface,比如UpdateInterface,它需要clientInstallTime,对于返回结果,需要比较版本号,检查升级逻辑,所以需要新的方法check

     比如AliveInterface,它需要clientInstallTime、totalForeroudTime、allowedPermission,这些信息在send中不能使用POST方法,而是要使用PUT,所以这个方法也得重写

整个模型参照这个设计即可。

最后进行编码实现,编码实现的时候,要把所有情况都遍历出来,比如UpdateInterface这个类,对于经的方法中的clientVer和serverVer,三种情况、clientVer > serverVer、clientVer == serverVer、clientVer < serverVer

最后,在整个包的入口位置对它们进行实例化,然后配置场景流程,最后就可以跑起来了,整个测试用例就是跑通状态,结果也就可靠了。

计划中的设计项:

    把相关测试的输入和输出信息,可以通过一个配置选项,控制输出到控制台或者写入到日志文件。这个应该也不复杂,配置一个全局类,搞一个公有变量开头就可以搞定的,应该很容易,下一步再实现一下,这样以下,我们的整个app的接口测试,就是完整的啦。。。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网速递520

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值