为什么没有捕获参数值?

时间:2017-12-01 03:17:40

标签: junit mockito

当我使用captor调用verify时,没有捕获任何参数值。但 如果我在开始时使用captor调用验证,则捕获者将获得所有值。谁能告诉我为什么?

public class AuthenticatorApplicationTest {
    @Captor ArgumentCaptor<String> captor;

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
     public void testAuthenticate() {
        AuthenticatorInterface authenticatorMock;
        CookBook1.AuthenticatorApplication authenticator;
        String username = "JavaCodeGeeks";
        String passwd = "unsafePassword";

        authenticatorMock = Mockito.mock(AuthenticatorInterface.class);
        authenticator = new CookBook1.AuthenticatorApplication(authenticatorMock);
        Mockito.when(authenticatorMock.authenticateUser(username, passwd)).thenReturn(false);
        boolean actual = authenticator.authenticate(username, passwd);

        Mockito.verify(authenticatorMock, Mockito.times(1)).authenticateUser(username, passwd);
        Mockito.verify(authenticatorMock, Mockito.atMost(1)).authenticateUser(captor.capture(), eq(passwd));
        Mockito.verify(authenticatorMock, Mockito.atLeast(1)).authenticateUser(username, passwd);
        Mockito.verify(authenticatorMock, Mockito.atMost(1)).authenticateUser(username, passwd);
        Mockito.verify(authenticatorMock, Mockito.atLeastOnce()).authenticateUser(username, passwd);


        System.out.println(captor.getAllValues());
        //Assert.assertEquals("xxx", captor.getValue(), "JavaCodeGeeks");
        //Assert.assertFalse(actual);
    }
}

0 个答案:

没有答案