找到你要的答案

Q:Mockito ArgumentCaptor Capture Conditional on Matcher

Q:Mockito ArgumentCaptor捕获条件匹配

In mockito we can capture arguments for a method that might be called many times like so:

verify(mocked, atLeastOnce())
    .myMethod(myCaptor.capture());

Then

myCaptor.getAllValues()

However then I would need to sift through all the captured values to find the one I am interested in for verification.

What I would like to do is something like this:

private class IsMySpecialArg extends ArgumentMatcher<Object> {
  public boolean matches(Object other) {
    // Matching condition
  }
}

...

verify(mocked, atLeastOnce())
        .myMethod(myCaptor.capture(argThat(new IsMySpecialArg()));

So that I can simply call myCaptor.getValue() and be assured it is referring to the argument I was actually interested in capturing. What is the best way to accomplish this is mockito, is it supported, or is there something fundamentally wrong with my testing strategy?

在mockito可以捕捉理由可以称为像许多倍的方法:

verify(mocked, atLeastOnce())
    .myMethod(myCaptor.capture());

然后

myCaptor.getAllValues()

然而,我需要筛选所有捕获的值,找到我感兴趣的验证。

我想做的是这样的事情:

private class IsMySpecialArg extends ArgumentMatcher<Object> {
  public boolean matches(Object other) {
    // Matching condition
  }
}

verify(mocked, atLeastOnce())
        .myMethod(myCaptor.capture(argThat(new IsMySpecialArg()));

所以,我可以简单的调用mycaptor。getvalue()放心是指论点实际上我感兴趣的捕捉。什么是最好的方法就是要mockito,它是支持的,或者是有什么与我的测试策略从根本上错了吗?

answer1: 回答1:

I use ArgumentMatchers pretty frequently when writing tests. Here's an example of what you can do when verifying the arguments that are passed to your mock objects:

private static class ExpectationArgumentMatcher extends ArgumentMatcher<String> {

    private final List<String> expectedArguments;

    public ExpectationArgumentMatcher(List<String> expectedArguments) {
        this.expectedArguments = expectedArguments;
    }

    public ExpectationArgumentMatcher() {
        this(new ArrayList<javax.swing.Action>());
    }


    @Override
    public boolean matches(Object argument) {
        if (argument instanceof String) {
            String actualArgument = (String) argument;
            return expectedArguments.contains(actualArgument);
        }
        return false;
    }
}

Obviously, you're expected arguments could just be a string, or something more sophisticated, but you're basically checking that your mock was called upon with an argument that matches your expected argument(s).

我用argumentmatchers相当频繁的写作测试的时候。这里是一个例子,你可以做什么,当验证的参数传递给你的模拟对象:

private static class ExpectationArgumentMatcher extends ArgumentMatcher<String> {

    private final List<String> expectedArguments;

    public ExpectationArgumentMatcher(List<String> expectedArguments) {
        this.expectedArguments = expectedArguments;
    }

    public ExpectationArgumentMatcher() {
        this(new ArrayList<javax.swing.Action>());
    }


    @Override
    public boolean matches(Object argument) {
        if (argument instanceof String) {
            String actualArgument = (String) argument;
            return expectedArguments.contains(actualArgument);
        }
        return false;
    }
}

显然,你预期的参数可能只是一个字符串,或更复杂的东西,但你基本上检查你的模拟调用的参数匹配你的预期参数(S)。

java  mockito