2

是否可以在 JMockit 中检查方法的局部变量?

来源

void foo(){
    boolean isPresent = false;
    /// ... something here sets isPresent
}

测试

can I check the value of isPresent at the end of call of foo() using JMockit?
4

1 回答 1

2

而不是尝试做一些晦涩的模拟机制。尝试将代码重构为可以测试的内容:

void foo(){
    boolean isPresent = isPresent();
}

boolean isPresent(){
   ....
}

另外,考虑一下。如果变量的值永远不会逃脱方法并且不会引起其他影响(应该是可测试的),为什么要尝试测试它?或者为什么它甚至在那里?测试方法范围变量的值为 x 没有任何值。测试该方法导致 y 因为变量是 x 具有值。

于 2011-10-10T03:05:39.903 回答