本文介绍了为什么可以在Spock中测试私有方法/字段而不会出现问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
package com.example.dev;
public class AClass {
private Integer a =10;
...//other code
}
,当我尝试在Spock方法中访问a时:
and when I try to access a in my Spock method:
package com.example.dev;
def 'test a'() {
AClass aClassVar = new AClass()
aClassVar.a = new Integer(100);
...//other testing stuff
}
工作正常.为什么会这样? Spock是否使用反射来访问私有字段?还是我的封装写得不好?
It works fine. Why this happens? Is Spock use reflection for accessing the private fields? Or my encapsulation in not written well?
推荐答案
Spock没罪,它本身就是令人讨厌的东西,请参阅:.
Spock isn't guilty, it's groovy itself, see: Private method in groovy is not private.
虽然可以引用班级的私人成员,但这绝对不是一个好习惯.
While it's possible to refer to private members of class, it's definitely not a good practice.
这篇关于为什么可以在Spock中测试私有方法/字段而不会出现问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!