throw checked Exceptions from mocks with Mockito

后端 未结 5 2054
梦毁少年i
梦毁少年i 2020-11-27 15:17

I\'m trying to have one of my mocked objects throw a checked Exception when a particular method is called. I\'m trying the following.

@Test(expectedException         


        
5条回答
  •  眼角桃花
    2020-11-27 15:53

    There is the solution with Kotlin :

    given(myObject.myCall()).willAnswer {
        throw IOException("Ooops")
    }
    

    Where given comes from

    import org.mockito.BDDMockito.given

提交回复
热议问题