JMock 意外调用

发布于 2024-12-16 02:11:58 字数 2029 浏览 3 评论 0原文

下面我只是尝试模拟一个名为 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)

Below I am just trying to mock a class named TestWrapper and set 'allowing' expecations on it. However, when setting expectations I get error. When using easymock and just setting expectations, this doesn't seem to happen

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;
     }
}

Assertion Error..

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2024-12-23 02:11:58

您错误地使用了 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 方法中存在编译错误

You're using the JMock API incorrectly. It should be

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));
    }});

}

This is saying that when you call the method under test (which you don't seem to be doing in your test) you will expect calls to those methods to return those values. The PaymentType would be a dependency in your class under test which you are mocking.

See JMock Getting Started

Also, you have a compliation error in the @Before method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文