我想访问特征中的私有(private)方法以进行单元测试,但我不知道该怎么做。
我有以下测试代码:
trait SomeTrait {
private String yell() { "YAAAH!" }
}
SomeTrait traitInstance = new Object() as SomeTrait
println traitInstance.yell()
在这个例子中我试图实现的是访问私有(private)方法并打印出“YAAAH!”,但我得到的是:
groovy.lang.MissingMethodException: No signature of method: Object15_groovyProxy.yell() is applicable for argument types: () values: []
Possible solutions: sleep(long), sleep(long, groovy.lang.Closure), wait(), every(), find(), dump() at ConsoleScript24.run(ConsoleScript24:7)
如何访问私有(private)方法?
最佳答案
你不能,as the documentation says :
Traits may also define private methods. Those methods will not appear in the trait contract interface:
您应该能够从其他公共(public)特征方法或特征中的静态方法调用它们,例如:
trait SomeTrait {
private String yell() { "YAAAH!" }
String doYell() { yell() }
}
SomeTrait traitInstance = new Object() as SomeTrait
println traitInstance.doYell()
关于groovy - 如何访问特征中的私有(private)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29752583/