博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Junit测试private方法
阅读量:4156 次
发布时间:2019-05-26

本文共 1405 字,大约阅读时间需要 4 分钟。

package com.bill99.junit;public class ACase {	private String echoRequest(String request) {		return "Hello!"+request;	}		private String echoRequest() {		return "Hello!";	}}

 

package com.bill99.junit;import java.lang.reflect.Method;import junit.framework.Assert;import org.junit.Before;import org.junit.Test;public class ACaseTest {	ACase a =null;		@Before	public void setUp() throws Exception {		a = new ACase();	}	@Test	public void testNoParamEchoRequest() throws Exception {		//测试没有参数的echoRequest()方法		Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest", null); 		//Method对象继承自java.lang.reflect.AccessibleObject,父类方法setAccessible可调		//将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查。                                          		//要访问私有方法必须将accessible设置为true,否则抛java.lang.IllegalAccessException		testNoParamMethod.setAccessible(true); 		//调用		Object result = testNoParamMethod.invoke(a, null);		System.out.println(result);		Assert.assertNotNull(result);			}		@Test	public void testParamEchoRequest() throws Exception {		//测试带有参数的echoRequest(String request)方法		Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest",String.class); 		testNoParamMethod.setAccessible(true); 		//调用		Object result = testNoParamMethod.invoke(a, "this is a test information");		System.out.println(result);		Assert.assertNotNull(result);			}}

 

转载地址:http://gckxi.baihongyu.com/

你可能感兴趣的文章
(python版)《剑指Offer》JZ01:二维数组中的查找
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
Mysql复制表以及复制数据库
查看>>
如何使用 systemd 中的定时器
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
Ribbon 学习(三):RestTemplate 请求负载流程解析
查看>>
深入理解HashMap
查看>>
XML生成(三):JDOM生成
查看>>
Ubuntu Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied)
查看>>
C#入门
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>