[英]Can we mock a spy object using mockito?
我有一個名為RadiationControl的類,並通過以下方式為其創建了一個間諜。
RadiationControl radCtrl = new RadiationControl();
RadiationControl spyRadCtrl = Mockito.spy(radCtrl);
我在另一個名為StationMonitor的類中有一個鏈接的方法調用,該類通過使用RadiationControl對象進行調用。 當我嘗試使用上面創建的間諜並嘗試訪問具有方法參數的參數時,它們會不時變化。
StationMonitorObject stationMonitorObject = radCtrl.getStationMonitorLoader().retrieveCVStationMonitorObject(Long.parseLong(syngId), status);
因此,使用上述語法,當我嘗試對該方法調用的間諜進行存根時,它抱怨存根已正確。
StationMonitorLoader stationMonitorLoader = StationMonitorLoader.getLoader(domain);
Mockito.doReturn(stationMonitorLoader).when(spyRadCtrl).getStationMonitorLoader();
Mockito.doReturn(stationMonitorObject).when(stationMonitorLoader).retrieveCVStationMonitorObject(any(Long.class), null);
有沒有更好的方法來解決這種情況?
有沒有更好的方法來解決這種情況?
是。
這里的問題是:
radCtrl.getStationMonitorLoader() .retrieveCVStationMonitorObject(Long.parseLong(syngId), status);
這違反了得墨meter耳的法律 (也不要和陌生人說話! )。
方法StationMonitorLoader
retrieveCVStationMonitorObject()
應該在RadiationControl
類中可用,並將調用委派給其依賴項(看起來像是StationMonitorLoader
...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.