KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mockobjects > TestExpectationSegment


1 package test.mockobjects;
2
3 import junit.framework.AssertionFailedError;
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6
7 import com.mockobjects.ExpectationSegment;
8 import com.mockobjects.util.TestCaseMo;
9
10 public class TestExpectationSegment extends TestCaseMo {
11     private static final Class JavaDoc THIS = TestExpectationSegment.class;
12
13     private ExpectationSegment myExpectation;
14
15     public TestExpectationSegment(String JavaDoc name) {
16         super(name);
17     }
18
19     public static void main(String JavaDoc[] args) {
20         start(new String JavaDoc[] { THIS.getName()});
21     }
22
23     public void setUp() {
24         myExpectation = new ExpectationSegment("Expectation segment");
25     }
26
27     public static Test suite() {
28         return new TestSuite(THIS);
29     }
30
31     public void testExpectNothing() {
32         myExpectation.setExpectNothing();
33
34         assertTrue("Should have an expectation", myExpectation.hasExpectations());
35     }
36
37     public void testExpectNothingFail() {
38         myExpectation.setExpectNothing();
39
40         boolean hasThrownException = false;
41         try {
42             myExpectation.setActual("some string");
43         } catch (AssertionFailedError ex) {
44             hasThrownException = true;
45         }
46
47         assertTrue("Should fail fast", hasThrownException);
48     }
49
50     public void testFailOnVerify() {
51         myExpectation.setExpected("a segment");
52         myExpectation.setFailOnVerify();
53
54         myExpectation.setActual("string without stuff");
55         assertVerifyFails(myExpectation);
56     }
57
58     public void testFailsImmediately() {
59
60         boolean hasThrownException = false;
61         myExpectation.setExpected("inner");
62         try {
63             myExpectation.setActual("String not containing segment");
64         } catch (AssertionFailedError expected) {
65             hasThrownException = true;
66         }
67
68         assertTrue("Should have thrown exception", hasThrownException);
69     }
70
71     public void testFlushActual() {
72         myExpectation.setActual("a string");
73
74         myExpectation.setExpectNothing();
75
76         myExpectation.verify();
77     }
78
79     public void testHasNoExpectations() {
80         myExpectation.setActual("a string");
81
82         assertTrue("Has no expectations", !myExpectation.hasExpectations());
83     }
84
85     public void testPasses() {
86
87         myExpectation.setExpected("inner");
88         myExpectation.setActual("String containing inner segment");
89
90         myExpectation.verify();
91     }
92 }
93
Popular Tags