2011-11-14 12 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

でのコンパイルにエラーを持っているJMock Getting Started

を参照してください。期待のパラメタを指定しますか? " –

答えて

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はあなたが嘲笑しているテスト対象のクラスの依存関係になります。

は、私は、これは、それは「あなたがたの」「に、モックメソッドを呼び出して要求されたときにメッセージがを暗示エラーのクラスであると考えている。また、あなたは@Beforeメソッド

+0

私はこれらのブレースに注意を払っていません。 Bedwyrを指してくれてありがとう! –

関連する問題