KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > MockObjectTestCase


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

3 package org.jmock;
4
5 import org.jmock.core.*;
6 import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;
7 import org.jmock.core.matcher.InvokeOnceMatcher;
8 import org.jmock.core.matcher.TestFailureMatcher;
9 import org.jmock.core.stub.ReturnStub;
10 import org.jmock.core.stub.StubSequence;
11 import org.jmock.core.stub.ThrowStub;
12
13
14 /**
15  * A base class for tests that use <a HREF="http://www.mockobjects.com">Mock Objects</a>.
16  * This class provides methods for creating mock objects and expectations and automatically
17  * verifying mock objects after the test has run, but before the test fixture has been torn down.
18  */

19 public abstract class MockObjectTestCase
20         extends MockObjectSupportTestCase
21 {
22     public MockObjectTestCase() {
23     }
24
25     public MockObjectTestCase( String JavaDoc name ) {
26         super(name);
27     }
28
29     public Mock mock( Class JavaDoc mockedType ) {
30         return mock(mockedType, defaultMockNameForType(mockedType));
31     }
32
33     public Mock mock( Class JavaDoc mockedType, String JavaDoc roleName ) {
34         Mock newMock = new Mock(newCoreMock(mockedType, roleName));
35         registerToVerify(newMock);
36         return newMock;
37     }
38
39     protected DynamicMock newCoreMock( Class JavaDoc mockedType, String JavaDoc roleName ) {
40         return new CoreMock(mockedType, roleName);
41     }
42
43     public String JavaDoc defaultMockNameForType( Class JavaDoc mockedType ) {
44         return "mock" + Formatting.classShortName(mockedType);
45     }
46
47     public Stub returnValue( Object JavaDoc o ) {
48         return new ReturnStub(o);
49     }
50
51     public Stub returnValue( boolean result ) {
52         return returnValue(new Boolean JavaDoc(result));
53     }
54
55     public Stub returnValue( byte result ) {
56         return returnValue(new Byte JavaDoc(result));
57     }
58
59     public Stub returnValue( char result ) {
60         return returnValue(new Character JavaDoc(result));
61     }
62
63     public Stub returnValue( short result ) {
64         return returnValue(new Short JavaDoc(result));
65     }
66
67     public Stub returnValue( int result ) {
68         return returnValue(new Integer JavaDoc(result));
69     }
70
71     public Stub returnValue( long result ) {
72         return returnValue(new Long JavaDoc(result));
73     }
74
75     public Stub returnValue( float result ) {
76         return returnValue(new Float JavaDoc(result));
77     }
78
79     public Stub returnValue( double result ) {
80         return returnValue(new Double JavaDoc(result));
81     }
82
83     public Stub throwException( Throwable JavaDoc throwable ) {
84         return new ThrowStub(throwable);
85     }
86
87     public InvocationMatcher once() {
88         return new InvokeOnceMatcher();
89     }
90
91     public InvocationMatcher atLeastOnce() {
92         return new InvokeAtLeastOnceMatcher();
93     }
94
95     public InvocationMatcher never() {
96         return new TestFailureMatcher("expect not called");
97     }
98
99     public Stub onConsecutiveCalls( Stub stub1, Stub stub2 ) {
100         return new StubSequence(new Stub[]{stub1, stub2});
101     }
102
103     public Stub onConsecutiveCalls( Stub stub1, Stub stub2, Stub stub3 ) {
104         return new StubSequence(new Stub[]{stub1, stub2, stub3});
105     }
106
107     public Stub onConsecutiveCalls( Stub stub1, Stub stub2, Stub stub3, Stub stub4 ) {
108         return new StubSequence(new Stub[]{stub1, stub2, stub3, stub4});
109     }
110 }
111
Popular Tags