1、测试私有方法
(1)使用反射

public class Calcutate {

    public int test() {
return add(2, 3);
} private int add(int a, int b) {
return a + b;
}
}
public class CalcutateTest {
@Test
public void testAdd() {
Calcutate cal = new Calcutate();
Object result = null;
try {
Method method = cal.getClass().getDeclaredMethod("add",
new Class[] { int.class, int.class });// 获得method.注意,这里不能使用getMethod方法,因为这个方法只能获取public修饰的方法..
method.setAccessible(true);// 这个设置为true.可以无视java的封装..不设置这个也无法或者这个Method
result = method.invoke(cal, new Object[] { 2, 10 }); } catch (Exception e) {
e.printStackTrace();
}
Assert.assertEquals("Must equals.", 13, result);// 这里自定拆箱..
}
}

method.setAccessible(true);

如果不显示设为true,则报错:

java.lang.IllegalAccessException: Class CalcutateTest can not access a member of class Calcutate with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.test.junit.CalcutateTest.testAdd(CalcutateTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Unit Test相关问题汇总的更多相关文章

  1. [转帖]xserver相关知识汇总

    xserver相关知识汇总 https://blog.csdn.net/QTVLC/article/details/81739984   本文主要是从以下几个方面介绍xorg-xserver 相关的知 ...

  2. Spark 1.x 爆内存相关问题汇总及解

    Spark 1.x 爆内存相关问题汇总及解决 OOM # 包括GC Overhead limitjava.lang.OutOfMemoryError # on yarn org.apache.hado ...

  3. Android开发环境——模拟器AVD相关内容汇总

     Android开发环境将分为SDK相关内容.Eclipse ADT相关内容.模拟器AVD相关内容.调试器DDMS相关内容.日志LogCat相关内容.连接驱动ADB相关内容.内存泄露检测工具MAT相关 ...

  4. Android开发环境——Eclipse ADT相关内容汇总

     Android开发环境将分为SDK相关内容.Eclipse ADT相关内容.模拟器AVD相关内容.调试器DDMS相关内容.日志LogCat相关内容.连接驱动ADB相关内容.内存泄露检测工具MAT相关 ...

  5. VS2012 Unit Test 个人学习汇总(含目录)

    首先,给出MSDN相关地址:http://msdn.microsoft.com/en-us/library/Microsoft.VisualStudio.TestTools.UnitTesting.a ...

  6. 【BO】SAP BO相关问题汇总贴

    本文将以往写过的关于SAP BO相关问题的帖子汇总了一下,方便寻找 #1 为WEBI报表添加自定义字体font #2 WEBI文件打开时提示Illegal access错误 #3 安装BO服务器时,o ...

  7. 关于OpenStack的学习路线及相关资源汇总

    首先我们想学习openstack,那么openstack是什么?能干什么?涉及的初衷是什么?由什么来组成?刚接触openstack,说openstack不是一个软件,而是由多个组件进行组合,这是一个更 ...

  8. 【汇总】涉及iOS&iPhone开发相关文章汇总

    此文章汇总本博客中有涉及iPhone开发的相关文章,不定时更新中... 1.NSUserDefaults快速存储数据: http://www.cnblogs.com/ios-wmm/archive/2 ...

  9. 合宙模块AT相关资料汇总

    1. 目录 1. 目录 [2. 新手教程](#2. 新手教程) [3. 产品资料](#3. 产品资料) 3.1 [2G模块(GPRS / GPRS+GNSS)](#3.1 2G模块(GPRS / GP ...

随机推荐

  1. Android中PopupWindow中有输入框时无法弹出输入法的解决办法

    PopupWindow window=new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); //必须让p ...

  2. 深度分析Linux下双网卡绑定七种模式

    现在一般的企业都会使用双网卡接入,这样既能添加网络带宽,同时又能做相应的冗余,可以说是好处多多.而一般企业都会使用linux操作系统下自带的网卡绑定模式,当然现在网卡产商也会出一些针对windows操 ...

  3. VC CArchive类使用

    CFile file("D:\\1.txt",CFile::modeCreate | CFile::modeWrite); CArchive ar(&file,CArchi ...

  4. PHP的输出缓冲区

    什么是缓冲区?简单而言,缓冲区的作用就是,把输入或者输出的内容先放进内存,而不显示或者读取.至于为什么要有缓冲区,这是一个很广泛的问题,如果有兴趣,可以在网山找下资料.其实缓冲区最本质的作用就是,协调 ...

  5. android之字体阴影效果

    今天刚刚好做了个字体阴影的效果,感觉加上了阴影的效果立体感十足啊!写了个简单的demo与大家分享下!主要是以下四个属性 android:shadowColor  阴影的颜色  android:shad ...

  6. easyui 获取cloumns字段

    var colums=datagrid.datagrid('options').columns; var frozens=datagrid.datagrid('options').frozenColu ...

  7. AJAX的概念介绍

    AJAX学习 1.XMLHttpRequest对象创建 var request= new XMLHttpRequest(); 兼容ie6.ie5 var request; if(windoe.XMLH ...

  8. Python进阶之路---1.5python数据类型-字符串

    字符串 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; ...

  9. 【反射】Reflect 介绍 示例

    介绍 JAVA反射机制是指:在运行状态中,对于任意一个[类],都能够知道这个类的所有属性和方法:对于任意一个[对象],都能够调用它的所有属性和方法:这种[动态]获取类中的信息以及动态调用对象的成员的功 ...

  10. lvm拉伸逻辑卷分区小总结

    文件系统                                      容量     已用      可用     已用% 挂载点 /dev/mapper/vg_znl-lv_root   ...