KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > VerifyingTestCaseTest


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

3 package test.jmock.core;
4
5 import junit.framework.TestCase;
6 import org.jmock.core.VerifyingTestCase;
7 import org.jmock.expectation.ExpectationCounter;
8 import test.jmock.core.testsupport.MockVerifiable;
9
10
11 public class VerifyingTestCaseTest extends TestCase
12 {
13     public static class ExampleTestCase extends VerifyingTestCase
14     {
15         private MockVerifiable verifiableField = new MockVerifiable();
16
17         public ExampleTestCase() {
18             setName("testMethod");
19         }
20
21         public void setExpectedVerifyCalls( int n ) {
22             verifiableField.verifyCalls.setExpected(n);
23         }
24
25         public void verifyExpectations() {
26             verifiableField.verifyExpectations();
27         }
28
29         public void testMethod() {
30             // Success!
31
}
32
33     }
34
35     public void testCanBeConstructedWithAName() {
36         String JavaDoc name = "NAME";
37
38         VerifyingTestCase testCase = new VerifyingTestCase(name)
39         {
40         };
41
42         assertEquals("name", name, testCase.getName());
43     }
44
45     public void testAutomaticallyVerifiesVerifiableFieldsAfterTheTestRunAndBeforeTearDown()
46             throws Throwable JavaDoc {
47         ExampleTestCase testCase = new ExampleTestCase();
48
49         testCase.setExpectedVerifyCalls(1);
50         testCase.runBare();
51         testCase.verifyExpectations();
52     }
53
54     public void testVerificationCanBeOverridden()
55             throws Throwable JavaDoc {
56         final ExpectationCounter overriddenVerifyCalls =
57                 new ExpectationCounter("overridden verify #calls");
58
59         ExampleTestCase testCase = new ExampleTestCase()
60         {
61             public void verify() {
62                 overriddenVerifyCalls.inc();
63             }
64         };
65
66         overriddenVerifyCalls.setExpected(1);
67         testCase.runBare();
68         overriddenVerifyCalls.verify();
69         testCase.verifyExpectations();
70     }
71
72     public void testOverridingRunTestDoesNotAffectVerification() throws Throwable JavaDoc {
73         ExampleTestCase testCase = new ExampleTestCase()
74         {
75             public void runTest() {
76             }
77         };
78
79         testCase.setExpectedVerifyCalls(1);
80         testCase.runBare();
81         testCase.verifyExpectations();
82     }
83
84     public void testOverridingSetUpAndTearDownDoesNotAffectVerification() throws Throwable JavaDoc {
85         ExampleTestCase testCase = new ExampleTestCase()
86         {
87             public void setUp() {
88             }
89
90             public void tearDown() {
91             }
92         };
93
94         testCase.setExpectedVerifyCalls(1);
95         testCase.runBare();
96         testCase.verifyExpectations();
97     }
98
99     public void testAutomaticallyVerifiesAnyObjectsRegisteredAsRequiringVerificatin() throws Throwable JavaDoc {
100         // setup
101
ExampleTestCase testCase = new ExampleTestCase();
102         MockVerifiable aVerifiable = new MockVerifiable();
103         MockVerifiable anotherVerifiable = new MockVerifiable();
104
105         // expect
106
aVerifiable.setExpectedVerifyCalls(1);
107         anotherVerifiable.setExpectedVerifyCalls(1);
108
109         // execute
110
testCase.registerToVerify(aVerifiable);
111         testCase.registerToVerify(anotherVerifiable);
112         testCase.runBare();
113
114         // verify
115
aVerifiable.verifyExpectations();
116         anotherVerifiable.verifyExpectations();
117     }
118
119     public void testAllowsVerifiableObjectsToBeUnregistered() throws Throwable JavaDoc {
120         // setup
121
ExampleTestCase testCase = new ExampleTestCase();
122         MockVerifiable aVerifiable = new MockVerifiable();
123
124         // expect
125
aVerifiable.setExpectedVerifyCalls(0);
126
127         // execute
128
testCase.registerToVerify(aVerifiable);
129         testCase.unregisterToVerify(aVerifiable);
130         testCase.runBare();
131
132         // verify
133
aVerifiable.verifyExpectations();
134     }
135 }
136
Popular Tags