首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JMock意外调用

JMock意外调用
EN

Stack Overflow用户
提问于 2011-11-14 23:49:00
回答 1查看 17.6K关注 0票数 4

下面我只是试图模拟一个名为TestWrapper的类,并在其上设置“allowing”期望。然而,在设置期望时,我得到了错误。当使用easymock和仅仅设置期望时,这似乎不会发生

代码语言:javascript
复制
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

代码语言:javascript
复制
 //Class I am mocking using JMock
 public class TestWrapper {

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

断言错误..

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-14 23:58:31

您错误地使用了JMock应用编程接口。它应该是

代码语言:javascript
复制
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 Getting Started

此外,在@Before方法中还存在编译错误

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8124143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档