KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExpectationSegment;
9
10
11 public class ExpectationSegmentTest extends TestCase
12 {
13
14     private ExpectationSegment myExpectation;
15
16     public void setUp() {
17         myExpectation = new ExpectationSegment("Expectation segment");
18     }
19
20     public void testExpectNothing() {
21         myExpectation.setExpectNothing();
22
23         assertTrue("Should have an expectation", myExpectation.hasExpectations());
24     }
25
26     public void testExpectNothingFail() {
27         myExpectation.setExpectNothing();
28
29         boolean hasThrownException = false;
30         try {
31             myExpectation.setActual("some string");
32         }
33         catch (AssertionFailedError ex) {
34             hasThrownException = true;
35         }
36
37         assertTrue("Should fail fast", hasThrownException);
38     }
39
40     public void testFailOnVerify() {
41         myExpectation.setExpected("a segment");
42         myExpectation.setFailOnVerify();
43
44         myExpectation.setActual("string without stuff");
45         AssertMo.assertVerifyFails(myExpectation);
46     }
47
48     public void testFailsImmediately() {
49
50         boolean hasThrownException = false;
51         myExpectation.setExpected("inner");
52         try {
53             myExpectation.setActual("String not containing segment");
54         }
55         catch (AssertionFailedError expected) {
56             hasThrownException = true;
57         }
58
59         assertTrue("Should have thrown exception", hasThrownException);
60     }
61
62     public void testFlushActual() {
63         myExpectation.setActual("a string");
64
65         myExpectation.setExpectNothing();
66
67         myExpectation.verify();
68     }
69
70     public void testHasNoExpectations() {
71         myExpectation.setActual("a string");
72
73         assertTrue("Has no expectations", !myExpectation.hasExpectations());
74     }
75
76     public void testPasses() {
77
78         myExpectation.setExpected("inner");
79         myExpectation.setActual("String containing inner segment");
80
81         myExpectation.verify();
82     }
83 }
84
Popular Tags