Ожидание JMock IllegalArgumentException

Я пытаюсь использовать метод allowing, чтобы исправить возвращаемое значение метода для одного из моих издевательских объектов. У меня есть следующий метод для макета объекта jdbc, который я хочу разрешить:

List<T> query(String sql, RowMapper<T> rowMapper)

Я использую with(any(RowMapper.class)), потому что мне все равно, какой RowMapper используется - я просто хочу убедиться, что когда он вызывает query с этим SQL, он возвращает List с "bob".

import java.util.Arrays;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.auto.Mock;
import org.jmock.integration.junit4.JUnitRuleMockery;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcOperations;

public class GetCustomersPhaseTest
{
    @Rule
    public final JUnitRuleMockery mockery = new JUnitRuleMockery();

    @Mock
    private SimpleJdbcOperations jdbc;

    @Mock
    private EventPublisher events;

    @Mock
    private Context context;

    private GetCustomersPhase phase;

    @Before
    public void setup()
    {
        phase = new GetCustomersPhase(jdbc, events);
    }

    @Test
    public void testGetCustomers()
    {
        mockery.checking(new Expectations() {
            {
                allowing(jdbc).query(with(equal("SELECT Name FROM Customers")), with(any(RowMapper.class)));
                will(returnValue(Arrays.asList("Bob")));
            }
        });

        phase.execute(context);
    }
}

Однако это дает мне,

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)

Я просмотрел этот вопрос и попытался решить проблему, но все еще не могу заставить ее работать .


person The Cat    schedule 28.11.2012    source источник
comment
Выглядит нормально для меня. Вы уверены, что определили правильное ожидание, которое терпит неудачу?   -  person Duncan Jones    schedule 29.11.2012
comment
Я предоставил еще немного кода, я использовал метод Hamcrest equalTo. Сейчас я использую метод equal из Expectations.   -  person The Cat    schedule 03.12.2012
comment
разобрались, глупые варарги!   -  person The Cat    schedule 03.12.2012


Ответы (2)


SimpleJdbcOperations#query принимает Object... в качестве последнего параметра. Ошибка возникает из-за того, что это не учитывается методом with, я думаю, это потому, что это компилируется для передачи пустого массива этому методу, который не учитывается.

Это работает, если вы используете,

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

Быстрая первая проверка, можете ли вы убедиться, что импортируете правильные методы with(), equalTo() и any()? иногда есть аналогичные методы из других пакетов.

person Steve Freeman    schedule 30.11.2012
comment
Я предоставил еще немного кода, я использовал метод Hamcrest equalTo. Сейчас я использую метод equal из Expectations. - person The Cat; 03.12.2012