Hi i am trying to mock the response i get from a Single observable that gets returned from retrofit using a delegator that my presenter class calls and i am getting the following error:

io.mockk.MockKException: no answer found for: LoginPresenter(#1).login(LoginRequest([email protected], password=password123))

Here is my test code

@Test fun testKotlinMock(){ val presenter : LoginPresenter = mockk<LoginPresenter>() val delegator = mockk<AccountDelegatorContract>() val viewCallback = mockk<LoginContract.LoginViewCallBack>() val cookieStore = mockk<PianoCookieStore>() val loginRequest = LoginRequest("[email protected]", "password123") val customerResponse = CustomerResponse("jon", "richy") every { delegator.login(loginRequest) } returns Single.just(Response.success(any())) every { delegator.getCustomer() } returns Single.just(customerResponse) every { presenter.loginViewCallBack } returns viewCallback every { presenter.accountDelegator } returns delegator every { presenter.cookieStorage } returns cookieStore presenter.login(loginRequest) } 

My actual Presenter code looks like this:

@Inject lateinit var loginViewCallBack: LoginViewCallBack @Inject lateinit var delegator: DelegatorContract @Inject lateinit var cookieStorage: CookieStore @Inject constructor() override fun login(loginRequest: LoginRequest) { delegator.login(loginRequest) .flatMap({ response -> saveCookieAndContinue(response) }) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(object : SingleObserver<CustomerResponse>{ override fun onSubscribe(d: Disposable) { } override fun onError(e: Throwable) { loginViewCallBack.onErrorLogin(PianoError.ERROR_LOGIN_INVALID) Log.d("JJJ", "login error") } override fun onSuccess(customerResponse : CustomerResponse) { loginViewCallBack.onLoginSuccess(customerResponse) Log.d("JJJ", "login successfully") } }) } private fun saveCookieAndContinue(response: Response<Void>): Single<CustomerResponse> { if (response.isSuccessful) { val headers = response.headers() cookieStorage.saveSessionCookies(headers.get(PianoCookieStore.COOKIE_HEADER_SET_NAME)!!) return accountDelegator.getCustomer() } //TODO: Change this to throw a login exception? throw RuntimeException() } 

i basically want to mock the injected dependencies you see from the main code and then run a happy path unit test.

It fails when i call the presenter.login(loginRequest) with the no answer found error

This is the kotlin extenstion plugin i am using

1

7 Answers

In your case you mocked the classes being tested. You have two options:

  • get rid of mockk for loginPresenter, just use original object and set properties
  • use spyk to create spy. This is something in between original object and mock

The exception is throw because mocks are strict by default, it just do not know how to handle it because mocks as objects are not initialized at all.

Read more about mocks, spies and relaxed mocks here:

2

First of all, I suggest you to debug your test. Then you will find which line of your code failed run. I get the same experienced with you, but in my case, my test failed when it reached on onSuccess, for example from your code is:

override fun onSuccess(customerResponse : CustomerResponse) { loginViewCallBack.onLoginSuccess(customerResponse) Log.d("JJJ", "login successfully") } 

I think from your test will fail after it hits line loginViewCallBack.onLoginSuccess(customerResponse), because loginViewCallback is not found in your mock test. If you have interface class that you want to mock, you should write it be:

@RelaxedMockK lateinit var viewCallback: LoginContract.LoginViewCallBack 

in my case, error not answer found error resolved after I changed this interface with relaxed mock.

from the docs: Relaxed mock is the mock that returns some simple value for all functions. This allows to skip specifying behavior for each case, while still allow to stub things you need. For reference types chained mocks are returned.

1

In my case I forgot to check that one method was called (for instance, in code we have object.setData(person.getAge())).

every { object.setData(any()) } just Runs // Mock method testedMethod() // Tested code runs here verify { object.setData(any()) } // Check that object.setData() was called 

In my case I have a function that access a setter but with Kotlin syntax and mockk was throwing an exception because you have to define the setter behavior. This can be done in two ways, depending on whether you want to read the value later or not.

If you want to read the value, you can setup a slot:

val myPropertySlot = slot<Int>() every { myClass.myProperty = capture(myPropertySlot) } just runs assertThat(myPropertySlot.captured).isEqualTo(1) 

If you just want the setter to run but you won't need the value, the syntax gets easier:

every { myClass.myProperty = any() } just runs 

This will make your test not throw if you try to access myProperty like this in the code being tested:

... myClass.myProperty = 1 ... 

If you are using mockk and looking for mocking void method then all you need to do is adding :

returns Unit 

or change it to this forms :

doNothing().`when`(mockedFile).write(any()) 

as described in

You should not mock the class under test. Using a spy is okay, though, if you need to verify a method was called, for example.

I would recommend to not using injection in classes you control. DI frameworks like Dagger are great for classes you don't create, such as Activities and Fragments, but for classes you do control, just use the constructor.

class LoginPresenter(private val loginViewCallBack: LoginViewCallBack, private val delegator: DelegatorContract, private val cookieStorage: CookieStore) { // rest of your code } 

Now you can easily provide a mock, or fake, to your login presenter. You also don't expose the dependencies. You could call presenter.delegator from your activity if you use injection, which you probably don't want.


Side note:

with your LoginPresenter using the constructor, and using dagger, you would create the presenter like:

class LoginModule { @Provides @ActivityScope internal providePresenter(loginViewCallBack: LoginViewCallBack, delegator: DelegatorContract, cookieStorage: CookieStore): LoginPresenter = LoginPresenter(loginViewCallBack, delegator, cookieStorage) } 

If you want to use injection instead, you just need to remember to set the mocks:

@Test fun `test authentication fails`() { val loginViewCallBack = mockk<LoginViewCallBack>() val delegator = mockk<DelegatorContract>() val cookieStorage = mockk<CookieStore>() val presenter = LoginPresenter() presenter.loginViewCallBack = loginViewCallBack presenter.delegator = delegator presenter.cookieStorage = cookieStorage val loginRequest: LoginRequest = ... //mock, fake, or real object every { delegator.login(loginRequest) } returns Single.error(RuntimeException("oops!")) presenter.login(loginRequest) verify { loginViewCallBack.onErrorLogin(PianoError.ERROR_LOGIN_INVALID) } } 

The example above will get rid of the "no answer found" for presenter.login(request) since presenter is no longer a mockk.

You could creating an empty test to reduce the problem to the test or context.

@Test fun `test without body for check context errors`() = runBlocking { } 

In my case, it was necessary to allow creation with no specific behaviour using @param relaxed


From: private val configuration = mockk<Configuration>()

To: private val configuration = mockk<Configuration>(relaxed = true)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.