1
import org.mockito.Mockito;

public class Scratch2 {
public static class Foo {
}

public interface Custom {
    public void someMethod(String arg1, String arg2, String arg3,
            String arg4);
}

public static class SomeClass {

    private final Custom custom;

    public SomeClass(Custom c) {
        this.custom = c;
    }

    public boolean run(Foo someFoo) {
        custom.someMethod("Dummy", "Dummy", "Dummy", "Dummy");
        return false;
    }
}

public static void callSomeMethod(Custom custom) {
    custom.someMethod("Dummy", "Dummy", "Dummy", "Dummy");
}

public static void main(String[] args) {
    Custom mock = Mockito.mock(Custom.class);
    SomeClass c = new SomeClass(mock);
    callSomeMethod(Mockito.mock(Custom.class));
    c.run(Mockito.any(Foo.class));

}
}

如果我们模拟自定义接口并直接在其上调用 someMethod 是没有问题的。但是使用 run() 会出现以下错误:

==========

线程“主”org.mockito.exceptions.misusing.InvalidUseOfMatchersException 中的异常:参数匹配器的使用无效!预期 4 个匹配器,1 个记录:-> 在 com.knewton.scratch.Scratch2.main(Scratch2.java:37)

如果匹配器与原始值组合,则可能发生此异常: //incorrect: someMethod(anyObject(), "raw String"); 使用匹配器时,所有参数都必须由匹配器提供。例如: //正确:someMethod(anyObject(), eq("String by matcher"));

有关更多信息,请参阅 Matchers 类的 javadoc。

在 com.knewton.scratch.Scratch2$SomeClass.run(Scratch2.java:24) 在 com.knewton.scratch.Scratch2.main(Scratch2.java:37)

===========

这是在模拟 1.9.5

4

2 回答 2

1

Your use of the matcher in this case is not correct. What happens is that mockito will record this matcher to use in a future call to a mocked method, which in your case happens here:

public boolean run(Foo someFoo) {
    custom.someMethod("Dummy", "Dummy", "Dummy", "Dummy"); // <- call on mock
    return false;
}

mockito counts your use of Mockito.any(Foo.class) for this method and now rightly complains that you only provided one matcher, but in fact, since the method requires 4 parameters, you need to pass 4 matchers.

Instead of passing a matcher to your run() method you should instead either pass a real instance (as you do in your own answer), or if that object is hard to instantiate and you don't need it anyway, you can replace it by a mock:

public static void main(String[] args) {
    Custom mock = Mockito.mock(Custom.class);
    SomeClass c = new SomeClass(mock);
    c.run(Mockito.mock(Foo.class));
}
于 2013-01-21T08:11:33.133 回答
0

所以这就是我如何解决它并且它很奇怪。它绝对是一个错误。

    import org.mockito.Mockito;

    public class Scratch2 {
        public static class Foo {
        }

        public interface Custom {
            public void someMethod(String arg1, String arg2, String arg3,
                    String arg4);
        }

        public static class SomeClass {

            private final Custom custom;

            public SomeClass(Custom c) {
                this.custom = c;
            }

            public boolean run(Foo serviceDiscoveryConfig) {
                custom.someMethod("Dummy", "Dummy", "Dummy", "Dummy");
                return false;
            }
        }

        public static void main(String[] args) {
            Custom mock = Mockito.mock(Custom.class);
            SomeClass c = new SomeClass(mock);
            Foo foo = new Foo();// HAD TO MAKE A NEW OBJECT HERE!
            c.run(foo);
        }
    }
于 2013-01-21T07:58:50.280 回答