Junit测试private方法
2015-12-27 02:40
344 查看
Java代码
package com.bill99.junit;
public class ACase {
private String echoRequest(String request) {
return "Hello!"+request;
}
private String echoRequest() {
return "Hello!";
}
}
Java代码
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);
}
}
package com.bill99.junit;
public class ACase {
private String echoRequest(String request) {
return "Hello!"+request;
}
private String echoRequest() {
return "Hello!";
}
}
Java代码
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);
}
}
相关文章推荐
- 任务栏 Adobe Acrobat DC / Adobe Reader 图标出错、异常、不显示(Windows下)
- 笔记本刚安装ubuntu不能连接wifi的问题
- 启动其他Activity并返回结果
- 给div设置一个关闭按钮.
- Activity切换中Intent传递数据、对象
- 自定义搭建PHP开发环境
- Yum安装nodejs + npm之CetnOS 6.5
- Looper类、主线程工作线程通信
- Handler进程之间通信
- JUnit编写单元测试代码注意点小结
- 一步一步学习SignalR进行实时通信_9_托管在非Web应用程序
- 底部RadioButton实现Fragment切换
- HDU 5602 Black Jack(博弈dp)
- 良好的异常展现界面<500.jsp>
- Centos7.1下安装Boost
- BNUOJ 51003 BQG's Confusing Sequence(二进制)
- linux网络命令--1--ifconfig
- leetcode笔记:Partition List
- Eclipse Debug 教程
- DBCP、C3P0、Proxool 、 BoneCP开源连接池的比较