KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > ExpectationCounter


1 package com.mockobjects;
2
3 import junit.framework.Assert;
4
5 public class ExpectationCounter extends AbstractExpectation {
6     private int myExpectedCalls = 0;
7     private int myActualCalls = 0;
8
9     public ExpectationCounter(String JavaDoc name) {
10         super(name);
11     }
12
13     public void clearActual() {
14         myActualCalls = 0;
15     }
16
17     public void inc() {
18         myActualCalls++;
19         if (shouldCheckImmediately()) {
20             Assert.assertTrue(
21                 myName + " should not be called more than " + myExpectedCalls + " times",
22                 myActualCalls <= myExpectedCalls);
23         }
24     }
25
26     public void setExpected(int expectedCalls) {
27         myExpectedCalls = expectedCalls;
28         setHasExpectations();
29     }
30
31     public void setExpectNothing() {
32         myExpectedCalls = 0;
33         setHasExpectations();
34     }
35
36     public void verify() {
37         assertEquals(
38             "did not receive the expected Count.",
39             myExpectedCalls,
40             myActualCalls);
41     }
42 }
43
Popular Tags