KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > expectation > ExpectationCounterTest


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package test.jmock.expectation;
4
5 import junit.framework.AssertionFailedError;
6 import junit.framework.TestCase;
7 import org.jmock.expectation.AssertMo;
8 import org.jmock.expectation.ExpectationCounter;
9
10
11 public class ExpectationCounterTest extends TestCase
12 {
13
14     public void testExpectNothing() {
15         ExpectationCounter e = new ExpectationCounter("");
16         e.setExpectNothing();
17
18         assertTrue("Has expectation", e.hasExpectations());
19         e.verify();
20     }
21
22     public void testExpectNothingFailure() {
23         ExpectationCounter e = new ExpectationCounter("");
24         e.setExpectNothing();
25
26         assertTrue("Has expectation", e.hasExpectations());
27         try {
28             e.inc();
29         }
30         catch (AssertionFailedError ex) {
31             return;
32         }
33         fail("Should have failed immediately");
34     }
35
36     public void testFailImmediately() {
37         ExpectationCounter aCounter = new ExpectationCounter("a test counter");
38         aCounter.setExpected(1);
39
40         aCounter.inc();
41         try {
42             aCounter.inc();
43         }
44         catch (AssertionFailedError ex) {
45             return;
46         }
47         fail("Should have failed immediately");
48     }
49
50     public void testFailOnVerify() {
51         ExpectationCounter aCounter = new ExpectationCounter("a test counter");
52         aCounter.setExpected(1);
53         aCounter.setFailOnVerify();
54
55         aCounter.inc();
56         aCounter.inc();
57
58         AssertMo.assertVerifyFails(aCounter);
59     }
60
61     public void testFailure() {
62         ExpectationCounter e = new ExpectationCounter("");
63         e.setExpected(1);
64
65         AssertMo.assertVerifyFails(e);
66     }
67
68     public void testFlushActual() {
69         ExpectationCounter e = new ExpectationCounter("");
70         e.inc();
71
72         e.setExpected(1);
73         e.inc();
74
75         e.verify();
76     }
77
78     public void testHasNoExpectations() {
79         ExpectationCounter aCounter = new ExpectationCounter("a test counter");
80
81         aCounter.inc();
82         assertTrue("Has no expectations", !aCounter.hasExpectations());
83     }
84
85     public void testSuccess() {
86         ExpectationCounter e = new ExpectationCounter("");
87         e.setExpected(1);
88         e.inc();
89
90         e.verify();
91     }
92 }
93
Popular Tags