以下範例為測試 PreMathM.all() 的行為:
- 呼叫MathM.returnOne()
- 檢驗return Value == 1
- 呼叫_mockMathM.echo(3)
- 檢驗return Value == 3
import org.jmock.Expectations; import org.jmock.Mockery; import org.jmock.Sequence; import org.jmock.integration.junit4.JMock; import org.jmock.integration.junit4.JUnit4Mockery; import org.jmock.lib.legacy.ClassImposteriser; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @SuppressWarnings("deprecation") @RunWith(JMock.class) public class JMockTest { Mockery _context = new JUnit4Mockery(){{ setImposteriser(ClassImposteriser.INSTANCE); }}; MathM _mockMathM = _context.mock(MathM.class); public class PreMathM{ MathM _mathm; public PreMathM(MathM mathm){ _mathm = mathm; } public void all(int echov){ _mathm.returnOne(); _mathm.echo(echov); } } public class MathM { public int returnOne() { return 1; } public int echo(int echov) { return echov; } } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testSequenceOneEcho() { final Sequence sequenceOneEcho = _context.sequence("sequenceOneEcho"); _context.checking(new Expectations() { { atLeast(1).of(_mockMathM).returnOne(); inSequence(sequenceOneEcho); will(returnValue(1)); // oneOf(_mockMathM).echo(3); atLeast(1).of(_mockMathM).echo(3); inSequence(sequenceOneEcho); will(returnValue(3)); } }); PreMathM mall = new PreMathM(_mockMathM); mall.all(3); } }上述範例中,每次呼叫inSequence(sequenceOneEcho),都是為了確保每一個行為是【按順序】執行呼叫
沒有留言:
張貼留言