Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JMock期望IllegalArgumentException_Java_Jmock - Fatal编程技术网

Java JMock期望IllegalArgumentException

Java JMock期望IllegalArgumentException,java,jmock,Java,Jmock,我正在尝试使用允许方法修复一个模拟对象上方法的返回值。我在模拟对象jdbc上有以下方法,我希望允许: List<T> query(String sql, RowMapper<T> rowMapper) 但是这给了我, java.lang.IllegalArgumentException: not all parameters were given explicit matchers: either all parameters must be specified by

我正在尝试使用
允许
方法修复一个模拟对象上方法的返回值。我在模拟对象
jdbc
上有以下方法,我希望允许:

List<T> query(String sql, RowMapper<T> rowMapper)
但是这给了我,

java.lang.IllegalArgumentException: not all parameters were given explicit matchers: either all parameters must be specified by matchers or all must be specified by values, you cannot mix matchers and values
    at org.jmock.internal.InvocationExpectationBuilder.checkParameterMatcherCount(InvocationExpectationBuilder.java:98)
    at org.jmock.internal.InvocationExpectationBuilder.createExpectationFrom(InvocationExpectationBuilder.java:91)
    at org.jmock.internal.InvocationToExpectationTranslator.invoke(InvocationToExpectationTranslator.java:19)
    at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
    at org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
    at $Proxy10.query(Unknown Source)
    at com.thecat.test.GetCustomersPhaseTest$1.<init>(GetCustomersPhaseTest.java:45)
    at com.thecat.test.GetCustomersPhaseTest.testGetCustomers(GetCustomersPhaseTest.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.jmock.integration.junit4.JUnitRuleMockery$1.evaluate(JUnitRuleMockery.java:49)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
java.lang.IllegalArgumentException:并非所有参数都提供了显式匹配器:要么所有参数都必须由匹配器指定,要么所有参数都必须由值指定,不能混合使用匹配器和值
位于org.jmock.internal.InvocationExpectationBuilder.checkParameterMatcherCount(InvocationExpectationBuilder.java:98)
位于org.jmock.internal.InvocationExpectationBuilder.createExpectationFrom(InvocationExpectationBuilder.java:91)
位于org.jmock.internal.InvocationToExpectationTranslator.invoke(InvocationToExpectationTranslator.java:19)
位于org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
位于org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
在$Proxy10.query处(未知来源)
在com.thecat.test.getCustomerPhaseTest$1。(getCustomerPhaseTest.java:45)
在com.thecat.test.GetCustomersPhaseTest.testGetCustomers(GetCustomersPhaseTest.java:43)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:601)
位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:45)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:42)
位于org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
位于org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
位于org.jmock.integration.junit4.JUnitRuleMockery$1.evaluate(JUnitRuleMockery.java:49)
位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:300)
位于org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
位于org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我看了这一点并试图解决这个问题,但我仍然无法让它工作。

首先快速检查,您是否可以确保使用()方法、equalTo()方法和任何()方法导入正确的内容?有时,其他软件包中也有类似的方法。

simplejbcoperations\35; query
对象…
作为最终参数。出现错误的原因是
with
方法没有考虑到这一点,我认为这是因为它编译为向这个方法传递一个空数组,而这个方法没有考虑到这一点

如果你使用

allowing(jdbc).query(with(equal("SELECT Name FROM Customers")), with(any(RowMapper.class)), with(equal(new Object[0])));
will(returnValue(Arrays.asList("Bob")));

我觉得还可以。你确定你已经确定了失败的正确预期吗?我提供了更多的代码,我使用的是Hamcrest
equalTo
方法。我现在使用的是
Expectations
中的
equal
方法。算了吧,傻瓦拉格!我提供了更多的代码,我使用的是Hamcrest
equalTo
方法。我现在使用的是
expections
中的
equal
方法。
allowing(jdbc).query(with(equal("SELECT Name FROM Customers")), with(any(RowMapper.class)), with(equal(new Object[0])));
will(returnValue(Arrays.asList("Bob")));