[英]How to mock some methods only throw exception for the first time and then do nothing, with specs2?
在specs2中,我們可以模擬方法並讓其引發異常:
class Hello {
def say():Unit = println("Hello, world")
}
val hello = mock[Hello]
hello.say() throws new RuntimeException("something wrong")
但是,如何使其僅在第一次拋出后始終什么也不做呢?
這實際上是一個模擬問題,而不是specs2問題。 從模擬文檔:
when(mock.someMethod("some arg"))
.thenThrow(new RuntimeException())
.thenReturn("foo");
可選的,較短版本的連續存根:
when(mock.someMethod("some arg"))
.thenReturn("one", "two", "three");
doThrow(new RuntimeException("something wrong")).doNothing().when(hello).say()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.