所以我嘗試測試方法methodUnderTest
是這樣的:忽略參數
methodUnderTest{
//Other stuff that works nice
this.setterMethod(OtherClass.methodThatErrors(arg1));
}
所以我想,以避免靜態方法調用methodThatErrors
。
我在做什麼至今:
ClassUnderTest spy = Mockito.spy(objectOfClassUnderTest);
Mockito.doNothing().when(spy).setterMethod(Mockito.any(OtherClass.class));
然而,這並不工作,方法仍然被調用。我會認爲它不應該調用參數方法,因爲我已經爲setterMethod
的參數寫了any
。那麼,如何使用Mockito而不是PowerMockito來阻止此通話。
編輯:
所以我設法通過移動靜態方法調用像這樣嘲笑它:
methodUnderTest{
this.extraSetterMethod(arg1);
}
extraSetterMethod(arg1){
this.setterMethod(OtherClass.methodThatErrors(arg1));
}
我真的不喜歡這種解決方案,因爲它添加的代碼幾個額外的行(甚至更多與javadoc),這只是使類更混亂。
是否有一個原因,你不想採取相反的方法,並從靜態類返回一個模擬,而不是嘲笑使用setter的行爲? – Makoto
@Makoto我以爲你不能用mockito來嘲笑靜態類嗎? – Aequitas
如果你使用PowerMockito,你可以。我的問題是你爲什麼從桌子上拿下這個選項。 – Makoto