Example of Mockito's argumentCaptor

后端 未结 4 1151
我在风中等你
我在风中等你 2020-12-04 07:21

Can anyone please provide me an example showing how to use the org.mockito.ArgumentCaptor class and how it is different from simple matchers that are p

相关标签:
4条回答
  • 2020-12-04 07:25

    Here I am giving you a proper example of one callback method . so suppose we have a method like method login() :

     public void login() {
        loginService = new LoginService();
        loginService.login(loginProvider, new LoginListener() {
            @Override
            public void onLoginSuccess() {
                loginService.getresult(true);
            }
    
            @Override
            public void onLoginFaliure() {
                loginService.getresult(false);
    
            }
        });
        System.out.print("@@##### get called");
    }
    

    I also put all the helper class here to make the example more clear: loginService class

    public class LoginService implements Login.getresult{
    public void login(LoginProvider loginProvider,LoginListener callback){
    
        String username  = loginProvider.getUsername();
        String pwd  = loginProvider.getPassword();
        if(username != null && pwd != null){
            callback.onLoginSuccess();
        }else{
            callback.onLoginFaliure();
        }
    
    }
    
    @Override
    public void getresult(boolean value) {
        System.out.print("login success"+value);
    }}
    

    and we have listener LoginListener as :

    interface LoginListener {
    void onLoginSuccess();
    
    void onLoginFaliure();
    

    }

    now I just wanted to test the method login() of class Login

     @Test
    public void loginTest() throws Exception {
        LoginService service = mock(LoginService.class);
        LoginProvider provider = mock(LoginProvider.class);
        whenNew(LoginProvider.class).withNoArguments().thenReturn(provider);
        whenNew(LoginService.class).withNoArguments().thenReturn(service);
        when(provider.getPassword()).thenReturn("pwd");
        when(provider.getUsername()).thenReturn("username");
        login.getLoginDetail("username","password");
    
        verify(provider).setPassword("password");
        verify(provider).setUsername("username");
    
        verify(service).login(eq(provider),captor.capture());
    
        LoginListener listener = captor.getValue();
    
        listener.onLoginSuccess();
    
        verify(service).getresult(true);
    

    also dont forget to add annotation above the test class as

    @RunWith(PowerMockRunner.class)
    @PrepareForTest(Login.class)
    
    0 讨论(0)
  • 2020-12-04 07:32

    I agree with what @fge said, more over. Lets look at example. Consider you have a method:

    class A {
        public void foo(OtherClass other) {
            SomeData data = new SomeData("Some inner data");
            other.doSomething(data);
        }
    }
    

    Now if you want to check the inner data you can use the captor:

    // Create a mock of the OtherClass
    OtherClass other = mock(OtherClass.class);
    
    // Run the foo method with the mock
    new A().foo(other);
    
    // Capture the argument of the doSomething function
    ArgumentCaptor<SomeData> captor = ArgumentCaptor.forClass(SomeData.class);
    verify(other, times(1)).doSomething(captor.capture());
    
    // Assert the argument
    SomeData actual = captor.getValue();
    assertEquals("Some inner data", actual.innerData);
    
    0 讨论(0)
  • 2020-12-04 07:42

    The two main differences are:

    • when you capture even a single argument, you are able to make much more elaborate tests on this argument, and with more obvious code;
    • an ArgumentCaptor can capture more than once.

    To illustrate the latter, say you have:

    final ArgumentCaptor<Foo> captor = ArgumentCaptor.forClass(Foo.class);
    
    verify(x, times(4)).someMethod(captor.capture()); // for instance
    

    Then the captor will be able to give you access to all 4 arguments, which you can then perform assertions on separately.

    This or any number of arguments in fact, since a VerificationMode is not limited to a fixed number of invocations; in any event, the captor will give you access to all of them, if you wish.

    This also has the benefit that such tests are (imho) much easier to write than having to implement your own ArgumentMatchers -- particularly if you combine mockito with assertj.

    Oh, and please consider using TestNG instead of JUnit.

    0 讨论(0)
  • 2020-12-04 07:49

    The steps in order to make a full check are:

    Prepare the captor :

    ArgumentCaptor<SomeArgumentClass> someArgumentCaptor = ArgumentCaptor.forClass(SomeArgumentClass.class);
    

    verify the call to dependent on component (collaborator of subject under test) times(1), is the default value, so ne need to add it.

    verify(dependentOnComponent, times(1)).send(someArgumentCaptor.capture());
    

    times(1) is the default, so no need to add it.

    Get the argument passed to collaborator

    SomeArgumentClass someArgument = messageCaptor.getValue();
    

    someArgument can be used for assertions

    0 讨论(0)
提交回复
热议问题