2012-11-28 2 views
2

allowing 메서드를 사용하여 내 조롱 된 개체 중 하나에서 메서드의 반환 값을 수정하려고합니다. - 난 그냥이 호출 할 때이 있는지 확인하려면 정말 RowMapper이 사용되는 상관하지 않기 때문에JMock 기대 IllegalArgumentException

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

내가 with(any(RowMapper.class))을 사용하고 있습니다 : 내가 허용하려는 조롱 객체 jdbc에 다음과 같은 방법을 해당 SQL이 query이면 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); 
    } 
} 

그러나 이것은 내가이 question에보고하고 문제를 해결하기 위해 노력하지만 여전히 캔트 작동 얻을

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) 

, 저를 제공합니다.

+0

나에게 확인을 찾습니다. 실패한 정확한 기대치를 확인 했습니까? –

+0

Ive가 더 많은 코드를 제공했으며 Hamcrest의 equalTo 메소드를 사용했습니다. 저는 이제'기대 '에서'평등'방법을 사용하고 있습니다. –

+0

그런 다음 그 문제를 해결 했습니까? –

답변

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"))); 
0

빠른 확인을 통해(), equalTo() 및 any() 메소드를 올바르게 가져올 수 있습니까? 때로는 다른 패키지와 비슷한 방법이 있습니다.

+0

필자는 더 많은 코드를 제공했으며, 나는 Hamcrest'equalTo' 메소드를 사용했다. 저는 이제'기대 '에서'평등'방법을 사용하고 있습니다. –