Hi All,
I'm just getting started with Mockito's argument captors and I'm getting a strange bug, that I can't quite figure out. Here is the full message:
No argument value was captured! You might have forgotten to use argument.capture() in verify()... ...or you used capture() in stubbing but stubbed method was not called. Be aware that it is recommended to use capture() only with verify() .
What's odd about this error as that all of arguments are matchers, all of the method calls are being done on Mocks, I'm only using verfiy, and when I step through the code I see that the method is being verified. I've tried different options, but so far none have been succesfull. This project is based on a sample of an MVP architecture implementation. I'll post a link to the project on github in a subsequent post. I'm trying to test the LoginPresenterImpl class.
Code sample:
package com.antonioleiva.mvpexample.app.main;
import com.antonioleiva.mvpexample.app.Login.LoginInteractor;
import com.antonioleiva.mvpexample.app.Login.LoginInteractorImpl;
import com.antonioleiva.mvpexample.app.Login.LoginPresenterImpl;
import com.antonioleiva.mvpexample.app.Login.LoginView;
import net.bytebuddy.dynamic.scaffold.TypeWriter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentCaptor.forClass;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Created by davidozersky on 2016-11-19.
*/
@RunWith(MockitoJUnitRunner.class)
public class LoginPresenterTest {
@Mock
LoginView loginView;
@Mock
private LoginInteractor loginInteractor;
@Mock
private LoginInteractor.OnLoginFinishedListener loginFinishedListener;
private LoginPresenterImpl loginPresenter;
@Before
public void setup() {
loginPresenter = new LoginPresenterImpl(loginView, loginInteractor);
}
@Test
public void testValidateCredentialsCallsLogin() {
ArgumentCaptor<String> usernameCaptor = forClass(String.class);
ArgumentCaptor<String> passwordCaptor = forClass(String.class);
ArgumentCaptor<LoginInteractor.OnLoginFinishedListener> loginInteractorArgumentCaptor = forClass(LoginInteractor.OnLoginFinishedListener.class);
loginPresenter.validateCredentials("username", "password");
verify(loginInteractor, times(1)).login(usernameCaptor.capture(),
passwordCaptor.capture(),
loginInteractorArgumentCaptor.capture());
assertThat(usernameCaptor.getValue(), is("username"));
assertThat(passwordCaptor.getValue(), is("password"));
assertThat(loginInteractorArgumentCaptor.getValue(), is((LoginInteractor.OnLoginFinishedListener) loginPresenter));
}
}