簡體   English   中英

如何使用正確的參數測試方法?

[英]How to test that a method was called with the correct parameter?

我的應用程序有兩個類, FireWatcherAlarmBell 當火災開始時,觀察者應該用一個水平響鈴。 對於小火,用小警報響鈴,對於大火,敲響鍾聲就像瘋了一樣。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM