Unable to use Arguement Captors, No argument value was captured!

2,667 views
Skip to first unread message

David Ozersky

unread,
Nov 20, 2016, 2:03:03 PM11/20/16
to mockito
Hi All, 

I'm new to Mockito,  and I'm trying to use Mockito to test a sample app demonstrating the MVP design pattern.  This is the error I get --> 

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()

Examples of correct argument capturing:
    ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
    verify(mock).doSomething(argument.capture());
    assertEquals("John", argument.getValue().getName());

I've been fiddling around with this for a few hours, and I can't figure out which requirement I'm not meeting.  I'm calling verify on stubs, I'm only using matchers as arguments, and all of the interactions are being done on mocks. 

Here's a link to the project.  
https://github.com/spark85/androidmvp/tree/development

Here is the code --> 

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));
}
}

Eric Lefevre-Ardant

unread,
Nov 20, 2016, 4:05:29 PM11/20/16
to moc...@googlegroups.com
I cannot really see what's wrong. Maybe a simplified version with the production code would help.
 
Any chance the login() method is not being called here for some reason?

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+unsubscribe@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/07be54f1-53c1-4be5-bae8-c5d3f9580f75%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Schwarz

unread,
Nov 22, 2016, 4:12:33 AM11/22/16
to mockito
Try to verify the call with argument matchers first and check if it fails or not. If it fails you know that the expected login never happened.

verify(loginInteractor).login(any(),any(),any());
Reply all
Reply to author
Forward
0 new messages