- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
下面我只是想模拟一个名为 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 将是您正在模拟的被测类中的依赖项。
此外,@Before 方法中存在编译错误
关于java - JMock 意外调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8124143/
jar jmock-2.5.1.jar 和 jmock-junit3-2.5.1.jar 有什么区别? jmock-junit 包含这两个框架吗? 谢谢 最佳答案 从jmock-junit3-2.5.
我正在尝试完成我的第一个 JMOCK 教程 http://www.jmock.org/getting-started.html ,而且进展并不顺利。 我遇到的问题如下: java.lang.Secur
我是 jMock 的新手,所以我正在一个简单的例子中尝试一下。但我不明白为什么它不起作用。这是我正在测试的类: package com.application; import com.domain.C
我有一些功能想要模拟从 main 调用(静态:我也读过 - jmock mocking a static method )。我最近读到 JMock 不支持静态函数的模拟。好吧,相关的代码(这给我带来了
在 JUNIT 测试中,我使用 JMOCK。在一些示例中,我看到了类似于以下代码片段的代码: mock.checking(new Expectations(){
阅读我看到的 jmock 文档: First we must import the jMock classes, define our test fixture class and create a
我正在使用junt和jock。假设我有一个对象接口(interface) Contact,并且在我的测试类中有一个类似这样的方法: @Test public void testAddOneContac
我正在使用 iBatis DAO 框架为应用程序编写测试。该应用程序在 java 1.4 环境中运行,因此我使用所有旧版本(JDK 1.4、JUnit3、iBatis 2.3 和 JMock 1.2)
是否可以为模拟对象赋值。例如: myMockObject = context.mock(MyObject.class); myMockObject.setId("someId"); 我正在测试的方法结
我正在为一个需要在其内部创建多个集合的类编写 JMock 测试。我为该类提供一个工厂,它将在需要时生成一个集合。 interface Factory { Collection newColl
我对 JUnit 测试类有以下期望: CustomEvent myCustomEvent = new CustomEvent(data1, data2); m_context.checking(new
我目前正在尝试做一个垄断游戏的模拟测试课。 我们已经得到了一些关于如何设置其中一些的说明,但也许我们只是误解了 JMock 的工作原理。 我们有使用 takeTurn() 的 Player 类。我们有
我要测试的方法是用不同的参数调用模拟方法: public void methodToTest(){ getMock().doSomething(1); getMock().doSometh
我正在努力学习如何编写测试。我也在学习 Java,有人告诉我应该学习/使用/练习 jMock,我在网上找到了一些有助于某些扩展的文章,例如: http://www.theserverside.com/
我目前正在编写一些涉及 JMock 的测试。我无法理解以下代码结构: context.checking(new Expectations() { //context is of type Mocke
下面我只是想模拟一个名为 TestWrapper 的类并在其上设置“允许”期望。但是,在设定期望时我会出错。当使用 easymock 并且只是设置期望时,这似乎并没有发生 import org.jmo
我正在设置一个模拟对象,每次调用方法 f() 时它应该返回一个新的业务对象。如果我简单地说 returnValue(new BusinessObj()),它将在每次调用时返回相同的引用。如果我不知道
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: jmock mocking a static method 我正在研究使用 XX 类静态方法的遗留单元测试。
我正在使用 JMock 来测试使用对象的类的行为。我想测试 a() 方法是否被调用。但是,b() 和 c() 也会在该对象上调用。因此,如果我期望 a(),它也必须期望 b() 和 c() 才能使测试
我的代码中有一个静态方法,我想以某种方式对其进行模拟。 我正在使用 jmock。 我想我可以做到这一点的一种方法是在静态方法周围有一个“包装类”,并且 mock 这个,但我希望有一个更好的解决方案。
我是一名优秀的程序员,十分优秀!