我面临的问题是Matchers.anyObject()回报null.当用于仅接受非可空类型的mock方法时,它会导致抛出"不应为null"异常.
`when`(mockedBackend.login(anyObject())).thenAnswer { invocationOnMock -> someResponse }
Run Code Online (Sandbox Code Playgroud)
模拟方法:
public open fun login(userCredentials: UserCredentials): Response
Run Code Online (Sandbox Code Playgroud) 我们可能知道,默认情况下,Kotlin类一旦定义,它就是最终的,除非它被明确声明open.
当我们想要使用Mockito进行模拟时,这会发出挑战.我们需要明确地声明它open.有没有办法可以避免将其声明为open能够模拟它进行测试?