你可以用...null
给定以下 Java 类:
public class Complex {
private final List<String> sideEffects = new ArrayList<>();
protected void sideEffect(String name) {
sideEffects.add("Side effect for " + name);
}
public int call(String name) {
sideEffect(name);
return name.length();
}
public List<String> getSideEffects() {
return sideEffects;
}
}
我们想要隐藏该方法,因此它不执行任何操作,因此我们可以使用以下规范:sideEffect
class ComplexSpec extends Specification {
def 'we can ignore void methods in Spies'() {
given:
Complex complex = Spy()
when:
int result = complex.call('tim')
then:
result == 3
1 * complex.sideEffect(_) >> null
complex.sideEffects == []
}
}