gpt4 book ai didi

java - JMock 意外调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:07 28 4
gpt4 key购买 nike

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

最佳答案

您未正确使用 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 Getting Started

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

关于java - JMock 意外调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8124143/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com