File tree Expand file tree Collapse file tree 2 files changed +8
-3
lines changed
Filter options
Expand file tree Collapse file tree 2 files changed +8
-3
lines changed
Original file line number Diff line number Diff line change @@ -46,6 +46,8 @@ stackoverflow-Java-top-qa
46
46
* [ StringBuilder和StringBuffer有哪些区别呢] ( https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/stringbuilder-and-stringbuffer.md )
47
47
* [ 为什么处理排序的数组要比非排序的快] ( https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md )
48
48
49
+ > 测试
50
+ * [ 如何测试 private 方法,变量或者内部类] ( https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md )
49
51
50
52
### 待翻译问题链接(还剩x问题)
51
53
- [ Why is subtracting these two times (in 1927) giving a strange result?] ( http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result )
Original file line number Diff line number Diff line change 1
- # 如何使用 junit 测试 private 方法,变量或者内部类
1
+ # 如何测试 private 方法,变量或者内部类
2
2
3
- 当你需要测试一个遗留的应用程序,且不能更改方法的可见性时,那测试私有方法 /属性的最好方式就是使用[ 反射] ( https://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 ) 。
3
+ 当你需要测试一个遗留的应用程序,且不能更改方法的可见性时,那么,测试私有方法 /属性的最好方式就是使用[ 反射] ( https://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 ) 。
4
4
5
- 实际测试时,可以通过一些反射辅助类设置和获取私有 (静态)的变量和调用私有 (静态)方法。遵循下面的窍门,你可以很好地处理私有方法和变量的测试。
5
+ 实际测试时,可以通过一些反射辅助类,设置和获取私有 (静态)的变量、调用私有 (静态)方法。遵循下面的窍门,你可以很好地处理私有方法和变量的测试。
6
6
7
7
```
8
8
Method method = targetClass.getDeclaredMethod(methodName, argClasses);
@@ -18,5 +18,8 @@ field.setAccessible(true);
18
18
field.set(object, value);
19
19
20
20
```
21
+ > note:
22
+ > 1 . ` targetClass.getDeclaredMethod(methodName, argClasses) ` 这个方法能让你获取到私有方法。` getDeclaredField ` 让你获取到私有变量
23
+ > 2 . 在对私有变量(方法)进行处理前,需要先` setAccessible(true) `
21
24
22
25
stackoverflow原址:http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes
You can’t perform that action at this time.
0 commit comments