2
我試圖驗證一個函數的調用不會導致與模擬的任何交互。但是,該模擬是在調用該函數之前,在類的構造函數。如何驗證從某個特定時刻開始,與模擬之間沒有更多的互動?
這是不行的,因爲有是與模擬互動:
SomeMock someMock = mock(SomeMock.class);
Subject subject = new Subject(someMock); // interactions with someMock happen here
subject.doNothingWithMock();
verifyNoMoreInteractions(someMock);
這是一個脆弱的解決方案:
SomeMock someMock = mock(SomeMock.class);
Subject subject = new Subject(someMock); // interactions with someMock happen here
verify(someMock).anInteraction();
verify(someMock).anotherInteraction();
subject.doNothingWithMock();
verifyNoMoreInteractions(someMock);
它的脆弱,因爲如果構造變化有其他與模擬的交互,即使測試不測試構造函數,也需要更改測試。
是否有替代方案?