4

下面我只是试图模拟一个名为 TestWrapper 的类并对其设置“允许”期望。但是,在设定期望时我会出错。当使用 easymock 并只是设置期望时,这似乎不会发生

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Before;
import org.junit.Test;

import java.math.BigDecimal;

public class CustomerPaymentProgramConverterTest {

    TestWrapper paymentType;

    Mockery mockery = new JUnit4Mockery() {{
            setImposteriser(ClassImposteriser.INSTANCE);
    }};

    @Before
    public void setupMethod() {

      paymentType = mockery.mock(TestWrapper.class);

    }

    @Test
    public void testFromWebService() {

        mockery.checking(new Expectations() {{

                    //debugger throws error on the line below.
                    allowing(paymentType.getScheduledPaymentAmount());
                    will(returnValue(new BigDecimal(123)));
                    allowing(paymentType.getScheduledPaymentConfirmationNumber());
                    will(returnValue(121212L));
        }});

    }
}

TestWrapper.class

 //Class I am mocking using JMock
 public class TestWrapper {

     public  java.math.BigDecimal getScheduledPaymentAmount() {
         return new BigDecimal(123);
     }
     public  long getScheduledPaymentConfirmationNumber() {
         return 123L;
     }
}

断言错误..

java.lang.AssertionError: unexpected invocation: paymentProgramScheduledPaymentTypeTestWrapper.getScheduledPaymentAmount()
no expectations specified: did you...
 - forget to start an expectation with a cardinality clause?
 - call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!
    at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
    at org.jmock.Mockery.dispatch(Mockery.java:218)
    at org.jmock.Mockery.access$000(Mockery.java:43)
    at org.jmock.Mockery$MockObject.invoke(Mockery.java:258)
4

1 回答 1

4

您错误地使用了 JMock API。它应该是

public void testFromWebService() {

    mockery.checking(new Expectations() {{

                //debugger throws error on the line below.
                allowing(paymentType).getScheduledPaymentAmount();
                will(returnValue(new BigDecimal(123)));
                allowing(paymentType).getScheduledPaymentConfirmationNumber();
                will(returnValue(121212L));
    }});

}

这就是说,当您调用被测方法时(您似乎在测试中没有这样做),您会期望调用这些方法来返回这些值。PaymentType 将是您正在模拟的被测类中的依赖项。

请参阅JMock 入门

此外,您在 @Before 方法中有一个编译错误

于 2011-11-14T15:58:30.767 回答