![](/img/trans.png)
[英]How to JUnit test that my mocked method is called with the parameter i want (not just id, but deep equal matcher)?
[英]How to test that a method was called with the correct parameter?
我的應用程序有兩個類, FireWatcher
和AlarmBell
。 當火災開始時,觀察者應該用一個水平響鈴。 對於小火,用小警報響鈴,對於大火,敲響鍾聲就像瘋了一樣。
class FireWatcher {
AlarmBell bell;
void onFire(int fireLevel) { bell.ring(2 * fireLevel); }
}
class AlarmBell {
void ring(int alarmLevel) { ... }
}
我想測試FireWatcher
以確保它調用具有正確級別的方法響鈴。 我怎么能和Mockito一起做?
我想要類似於以下內容,但在文檔中找不到任何內容。
when(fireWatcher.onFire(1)).expect(mockAlarmBell.ring(2));
你需要傳入一個AlarmBell
。
例:
@Test
public void watcherShouldRingTheAlarmBellWhenOnFire() {
AlarmBell alarm = mock(AlarmBell.class);
FireWatcher watcher = new FireWatcher(alarm);
watcher.onFire(1);
verify(alarm).ring(2);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.