How to use ReturningValuesAcceptanceTests class of org.jmock.test.acceptance package

Best Jmock-library code snippet using org.jmock.test.acceptance.ReturningValuesAcceptanceTests

Run Jmock-library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*  Copyright (c) 2000-2004 jMock.org
2 */
3package org.jmock.test.acceptance;
4
5import java.util.Date;
6
7import junit.framework.TestCase;
8
9import org.jmock.Expectations;
10import org.jmock.Mockery;
11
12
13public class ReturningValuesAcceptanceTests extends TestCase {
14    public interface ReturnTypes {
15        String returnString();
16        boolean returnBoolean();
17        byte returnByte();
18        char returnChar();
19        short returnShort();
20        int returnInt();
21        long returnLong();
22        float returnFloat();
23        double returnDouble();
24        void voidMethod();
25    }
26
27    private Mockery context = new Mockery();
28    private ReturnTypes mock = context.mock(ReturnTypes.class, "mock");
29
30    public void testCanReturnObjectReferences() {
31        // ensure string is not interned
32        final String result = new String("RESULT");
33
34        context.checking(new Expectations() {{
35            allowing(mock).returnString();
36            will(returnValue(result));
37        }});
38
39        assertSame("should be same result", result, mock.returnString());
40    }
41
42    public void testCanReturnNullObjectReferences() {
43        context.checking(new Expectations() {{
44            allowing(mock).returnString(); will(returnValue(null));
45        }});
46
47        assertNull("should be null", mock.returnString());
48    }
49
50    public void testCanReturnBooleanValues() {
51        context.checking(new Expectations() {{
52            exactly(1).of(mock).returnBoolean(); will(returnValue(true));
53            exactly(1).of(mock).returnBoolean(); will(returnValue(false));
54        }});
55        
56        assertTrue("should be true", mock.returnBoolean());
57        assertFalse("should be false", mock.returnBoolean());
58    }
59
60    public void testCanReturnByteValues() {
61        final byte result = 123;
62
63        context.checking(new Expectations() {{
64            allowing(mock).returnByte(); will(returnValue(result));
65        }});
66
67        assertEquals("should be same result", result, mock.returnByte());
68    }
69
70    public void testCanReturnCharValues() {
71        final char result = '\u1234';
72
73        context.checking(new Expectations() {{
74            allowing(mock).returnChar(); will(returnValue(result));
75        }});
76
77        assertEquals("should be same result", result, mock.returnChar());
78    }
79
80    public void testCanReturnShortValues() {
81        final short result = 12345;
82
83        context.checking(new Expectations() {{
84            allowing(mock).returnShort(); will(returnValue(result));
85        }});
86
87        assertEquals("should be same result", result, mock.returnShort());
88    }
89
90    public void testCanReturnIntValues() {
91        final int result = 1234567890;
92
93        context.checking(new Expectations() {{
94            allowing(mock).returnInt(); will(returnValue(result));
95        }});
96
97        assertEquals("should be same result", result, mock.returnInt());
98    }
99
100    public void testCanReturnLongValues() {
101        final long result = 1234567890124356789L;
102
103        context.checking(new Expectations() {{
104            allowing(mock).returnLong(); will(returnValue(result));
105        }});
106
107        assertEquals("should be same result", result, mock.returnLong());
108    }
109
110    public void testCanReturnFloatValues() {
111        final float result = 12345.67890f;
112
113        context.checking(new Expectations() {{
114            allowing(mock).returnFloat(); will(returnValue(result));
115        }});
116
117        assertEquals("should be same result", result, mock.returnFloat(), 0.0);
118    }
119
120    public void testCanReturnDoubleValues() {
121        final double result = 1234567890.1234567890;
122
123        context.checking(new Expectations() {{
124            allowing (mock).returnDouble(); will(returnValue(result));
125        }});
126
127        assertEquals("should be same result", result, mock.returnDouble(), 0.0);
128    }
129    
130    public void testWillReturnADefaultValueIfNoResultExplicitlySpecified() {
131        context.checking(new Expectations() {{
132            allowing (mock).returnInt();
133        }});
134        
135        // This will not throw a NullPointerException
136        mock.returnInt();
137    }
138
139    public class Something {}
140    
141    public interface AnInterfaceThatReturnsSomething {
142        Something returnSomething();
143    }
144    
145    public void testReturnsNullAsTheDefaultValueForUnregisteredType() {
146        final AnInterfaceThatReturnsSomething returningMock = context.mock(AnInterfaceThatReturnsSomething.class, "returningMock");
147        
148        context.checking(new Expectations() {{
149            allowing (returningMock).returnSomething();
150        }});
151        
152        Something defaultResult = returningMock.returnSomething();
153        
154        assertNull("returned null", defaultResult);
155    }
156    
157    public void testCanDefineDefaultReturnValuesForUnregisteredTypes() {
158        final AnInterfaceThatReturnsSomething returningMock = context.mock(AnInterfaceThatReturnsSomething.class, "returningMock");
159        
160        Something expectedDefaultResult = new Something();
161        
162        context.setDefaultResultForType(Something.class, expectedDefaultResult);
163        
164        context.checking(new Expectations() {{
165            allowing (returningMock).returnSomething();
166        }});
167        
168        Something defaultResult = returningMock.returnSomething();
169        
170        assertSame("returned the default result", expectedDefaultResult, defaultResult);
171    }
172    
173    public void testCanChangeDefaultReturnValueForRegisteredType() {
174        String newDefaultString = "hoo-hee-haa-haa";
175        context.setDefaultResultForType(String.class, newDefaultString);
176        
177        context.checking(new Expectations() {{
178            allowing (mock).returnString();
179        }});
180        
181        assertSame("returned the default result", newDefaultString, mock.returnString());
182    }
183
184    public void testReportsTypeMismatchOfResults() {
185        try {
186            context.checking(new Expectations() {{
187                allowing (mock).returnString(); will(returnValue(new Date()));
188            }});
189            
190            mock.returnString();
191            fail("Should have thrown IllegalStateException");
192
193        } catch (IllegalStateException expected) {
194        }
195    }
196    
197    public void testReportsTypeMismatchWhenValuesReturnedFromVoidMethods() {
198        context.checking(new Expectations() {{
199            allowing (mock).voidMethod(); will(returnValue("wrong result"));
200        }});
201        
202       try {
203           mock.voidMethod();
204           fail("Should have thrown IllegalStateException");
205
206       } catch (IllegalStateException expected) {
207       }
208    }
209    
210}
211
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)