2011-11-14 50 views
3

下面我只是想模擬一個名爲TestWrapper的類,並設置'允許'的預期。但是,在設定期望值時我會遇到錯誤。當使用EasyMock的,只是設定的預期,這似乎並沒有發生JMock意外的調用

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) 
+0

我相信這是類錯誤的消息暗示,它要求當「你」,「調用模擬的方法來指定期望的參數?「 –

回答

3

您使用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方法有compliation錯誤

+0

我只是注意那些大括號。感謝您指點Bedwyr! –