junit私有方法测试
2016-06-23 17:22
323 查看
很多人说类的私有方法不用测试,理由是类私有方法只允许被本类访问,而其他类无权调用。事实上并非如此,来看一个范例程序,Commodity_Parent是一个商品父类,定义了两个商品类基础属性:commodity_name,commodity_price,分别是商品的名称和价格。二组公共和两组受保护的实例方法分别是getcommodity_name(),getcommodity_price,setcommodity_name(),setcommodity_price(),对应的功能是取商品名称、价格,设置商品名称、价格。具体代码如下:
package com.fastpoint.Private;
public class Commodity_Parent {
private String commodity_name;
private Double commodity_price;
public Commodity_Parent(String name, Double price) {
commodity_name = name;
commodity_price = price;
}
public String getCommodity_name() {
return commodity_name;
}
public void setCommodity_name(String commodity_name) {
this.commodity_name = commodity_name;
}
public Double getCommodity_price() {
return commodity_price;
}
public void setCommodity_price(Double commodity_price) {
this.commodity_price = commodity_price;
}
}
Commodity_Child类继承了Commodity_Parent类,并且增加了一个新的类属性Commodity_number,该属性用来记录具体商品类的计量数量。GetCommodityNumber()方法用来取得该类实例的商品数量,对应的SetCommodityNumber()实例方法被声明为私有的,从这里可以看出,程序不允许别人使用该方法对商品数量做直接的修改,而是通过一个共有的setData()方法实现更安全的商品数量操作,具体代码如下:
package com.fastpoint.Private;
public class Commodity_Child extends Commodity_Parent {
private Integer Commodity_number;
public Commodity_Child(String Commodity_name, double Commodity_price,
Integer number) {
super(Commodity_name, Commodity_price);
Commodity_number = number;
}
public Integer getCommodity_number() {
return Commodity_number;
}
private void setCommodity_number(Integer commodity_number) {
Commodity_number = commodity_number;
}
public void setData(Integer commoditynumber) {
if (isValidate()) {
if (isCommodityNumber(commoditynumber)) {
setCommodity_number(commoditynumber);
} else {
System.out.println("没有足够的商品数量!");
}
} else {
System.out.println("没有足够权限!");
}
}
public boolean isValidate() {
//示例程序 简化部分代码
return true;
}
public boolean isCommodityNumber(double d) {
//示例程序 简化部分代码
return true;
}
}
可以利用java的反射机制来实现访问类的私有方法或者属性。
使用java.lang.Class的相关方法,获得相关指定对象的Field,然后调用field.setAccessible(true);绕过访问权限的检查,然后可以访问Field的值,当然也可以设置Field的值。
使用java.lang.Class的相关方法,获得相关指定对象的Method;然后调用field.setAccessible(true);绕过访问权限的检查;最后执行该方法.
测试代码如下所示:
package com.fastpoint.testPrivate;
import static org.junit.Assert.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class Commodity_ChildTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testSetCommodity_number() throws ClassNotFoundException,
SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException,
InstantiationException {
Commodity_Child comm = new Commodity_Child();
Class[] parameterTypes = new Class[1];// 这里你要调用的方法只有一个参数
parameterTypes[0] = Integer.class;// 参数类型为String[]
Method method = comm.getClass().getDeclaredMethod(
"setCommodity_number", Integer.class);// 要调用的方法是SetCommodity_number
Object[] args = new Object[1];
method.setAccessible(true);// 允许处理私有方法
method.invoke(comm, new Object[] { new Integer(15) });// 调用方法
method.setAccessible(false);
assertEquals(15, comm.getCommodity_number(), 0);
}
}
上面代码演示了测试Commodity_Child类中的私有方法setCommodity_Number(),主要运用了Java的反射机制。
专注于自动化、性能研究,博客为原创,转载请注明文章来源于:http://www.cnblogs.com/Automation_software/ 只求在IT界有一个清闲的世界让我静心的去专研,不求功名利禄,只为心中的那份成就感及自我成长、自我实现的快感。
package com.fastpoint.Private;
public class Commodity_Parent {
private String commodity_name;
private Double commodity_price;
public Commodity_Parent(String name, Double price) {
commodity_name = name;
commodity_price = price;
}
public String getCommodity_name() {
return commodity_name;
}
public void setCommodity_name(String commodity_name) {
this.commodity_name = commodity_name;
}
public Double getCommodity_price() {
return commodity_price;
}
public void setCommodity_price(Double commodity_price) {
this.commodity_price = commodity_price;
}
}
Commodity_Child类继承了Commodity_Parent类,并且增加了一个新的类属性Commodity_number,该属性用来记录具体商品类的计量数量。GetCommodityNumber()方法用来取得该类实例的商品数量,对应的SetCommodityNumber()实例方法被声明为私有的,从这里可以看出,程序不允许别人使用该方法对商品数量做直接的修改,而是通过一个共有的setData()方法实现更安全的商品数量操作,具体代码如下:
package com.fastpoint.Private;
public class Commodity_Child extends Commodity_Parent {
private Integer Commodity_number;
public Commodity_Child(String Commodity_name, double Commodity_price,
Integer number) {
super(Commodity_name, Commodity_price);
Commodity_number = number;
}
public Integer getCommodity_number() {
return Commodity_number;
}
private void setCommodity_number(Integer commodity_number) {
Commodity_number = commodity_number;
}
public void setData(Integer commoditynumber) {
if (isValidate()) {
if (isCommodityNumber(commoditynumber)) {
setCommodity_number(commoditynumber);
} else {
System.out.println("没有足够的商品数量!");
}
} else {
System.out.println("没有足够权限!");
}
}
public boolean isValidate() {
//示例程序 简化部分代码
return true;
}
public boolean isCommodityNumber(double d) {
//示例程序 简化部分代码
return true;
}
}
可以利用java的反射机制来实现访问类的私有方法或者属性。
使用java.lang.Class的相关方法,获得相关指定对象的Field,然后调用field.setAccessible(true);绕过访问权限的检查,然后可以访问Field的值,当然也可以设置Field的值。
使用java.lang.Class的相关方法,获得相关指定对象的Method;然后调用field.setAccessible(true);绕过访问权限的检查;最后执行该方法.
测试代码如下所示:
package com.fastpoint.testPrivate;
import static org.junit.Assert.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class Commodity_ChildTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testSetCommodity_number() throws ClassNotFoundException,
SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException,
InstantiationException {
Commodity_Child comm = new Commodity_Child();
Class[] parameterTypes = new Class[1];// 这里你要调用的方法只有一个参数
parameterTypes[0] = Integer.class;// 参数类型为String[]
Method method = comm.getClass().getDeclaredMethod(
"setCommodity_number", Integer.class);// 要调用的方法是SetCommodity_number
Object[] args = new Object[1];
method.setAccessible(true);// 允许处理私有方法
method.invoke(comm, new Object[] { new Integer(15) });// 调用方法
method.setAccessible(false);
assertEquals(15, comm.getCommodity_number(), 0);
}
}
上面代码演示了测试Commodity_Child类中的私有方法setCommodity_Number(),主要运用了Java的反射机制。
专注于自动化、性能研究,博客为原创,转载请注明文章来源于:http://www.cnblogs.com/Automation_software/ 只求在IT界有一个清闲的世界让我静心的去专研,不求功名利禄,只为心中的那份成就感及自我成长、自我实现的快感。
相关文章推荐
- Shell 脚本编程陷阱
- Mootools 1.2教程 函数
- autoit InputBox 函数
- VBS脚本写的Windows硬件检测工具分享
- 文件遍历排序函数
- 用vbscript实现隐藏任务栏图标的脚本
- 用autoit编写第一个脚本(Hello World)
- VBS调用WMI快速关闭IE的脚本
- 关于C#中排序函数的总结
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- Oracle数据库执行脚本常用命令小结
- 收集的ROS防火墙脚本
- JSP脚本漏洞面面观
- 不错的批处理脚本 第一部分
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- VBS脚本加密/解密VBS脚本(简易免杀版1.1)
- 不错的批处理脚本实例代码 第二部分
- DOS批处理 函数定义与用法