junit私有方法测试

摘要:
很多人说类的私有方法不用测试,理由是类私有方法只允许被本类访问,而其他类无权调用。");}}else{System.out.println("没有足够权限!");}}publicbooleanisValidate(){//示例程序简化部分代码returntrue;}publicbooleanisCommodityNumber{//示例程序简化部分代码returntrue;}}可以利用java的反射机制来实现访问类的私有方法或者属性。

很多人说类的私有方法不用测试,理由是类私有方法只允许被本类访问,而其他类无权调用。事实上并非如此,来看一个范例程序,Commodity_Parent是一个商品父类,定义了两个商品类基础属性:commodity_name,commodity_price,分别是商品的名称和价格。二组公共和两组受保护的实例方法分别是getcommodity_name(),getcommodity_price,setcommodity_name(),setcommodity_price(),对应的功能是取商品名称、价格,设置商品名称、价格。具体代码如下:

package com.fastpoint.Private;

publicclass 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;

}

publicvoid setCommodity_name(String commodity_name) {

this.commodity_name = commodity_name;

}

public Double getCommodity_price() {

return commodity_price;

}

publicvoid setCommodity_price(Double commodity_price) {

this.commodity_price = commodity_price;

}

}

Commodity_Child类继承了Commodity_Parent类,并且增加了一个新的类属性Commodity_number,该属性用来记录具体商品类的计量数量。GetCommodityNumber()方法用来取得该类实例的商品数量,对应的SetCommodityNumber()实例方法被声明为私有的,从这里可以看出,程序不允许别人使用该方法对商品数量做直接的修改,而是通过一个共有的setData()方法实现更安全的商品数量操作,具体代码如下:

package com.fastpoint.Private;

publicclass 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;

}

privatevoid setCommodity_number(Integer commodity_number) {

Commodity_number = commodity_number;

}

publicvoid setData(Integer commoditynumber) {

if (isValidate()) {

if (isCommodityNumber(commoditynumber)) {

setCommodity_number(commoditynumber);

} else {

System.out.println("没有足够的商品数量!");

}

} else {

System.out.println("没有足够权限!");

}

}

publicboolean isValidate() {

//示例程序简化部分代码

returntrue;

}

publicboolean isCommodityNumber(double d) {

//示例程序简化部分代码

returntrue;

}

}

可以利用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的反射机制。

免责声明:文章转载自《junit私有方法测试》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇spark程序设计ClearCanvas DICOM 开发系列 一下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java单元测试 Http Server Mock框架选型

背景动机 某期优化需要针对通用的HttpClient封装组件--HttpExecutor在保证上层暴露API不动的前提做较多改动,大致包括以下几点: apache http client 版本升级 HttpClientBuilder代码重构 RequestBuilder代码重构 自定义RetryHandler HttpContext扩展 自定义HttpR...

1.如何在JMeter中使用JUnit

您是否需要在测试过程中使用JUnit?要回答这个问题,我们先来看看单元测试。 单元测试是软件测试生命周期中测试的最低分辨率。运行单元测试时,需要在应用程序中使用最小的可测试功能,将其与其他代码隔离,并确定其行为是否符合预期。这使您可以在开始构建更大的模块之前验证正在测试的应用程序的小“单元”是否正常工作。 Junit是迄今为止最流行的Java语言单元测试框...

【Java】VS Code导入jar包及进行JUnit单元测试

如何配置 VS Code 下的 Java 开发环境就不细说了,只要安装好了 JDK,安装一个插件 Java Extension Pack 就行了,它会自动安装几个微软推荐的插件,基础的 Java 环境就搭好了。 进行 JUnit 单元测试需要的 jar 包:junit.jar 和 hamcrest-core.jar,链接如下: https://github...

JUnit入门

一、简单介绍 JUnit是一个开源的java单元測试框架。在1997年,由 Erich Gamma 和 Kent Beck 开发完毕。这两个牛人中 Erich Gamma 是 GOF 之中的一个;Kent Beck 则在 XP 中有重要的贡献(你认为眼熟一点都不奇怪)。        正如常言道:“麻雀虽小,五脏俱全。” JUnit设计的很小巧,可是功能...

Selenium WebDriver问题Internet Explorer保护模式设置问题

在用WebDriver中打开Internet Explorer访问百度的是,报下面错误: org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Protected Mode settings are not the...

vscode wsl2使用junit测试java

首先你要安装有插件 Java Extension Pack 然后下载两个文件,Junit模块支持需要两个文件:hamcrest&JUnit https://github.com/junit-team/junit4/wiki/Download-and-Install 在当前代码目录下建立文件夹lib,把两个放进去。...