How to use method of org.jmock.test.acceptance.ErrorMessagesAcceptanceTests class

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

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.test.acceptance;
2
3import junit.framework.TestCase;
4import org.hamcrest.StringDescription;
5import org.jmock.Expectations;
6import org.jmock.Mockery;
7import org.jmock.api.Action;
8import org.jmock.api.ExpectationError;
9import org.jmock.api.Invocation;
10import org.jmock.lib.action.CustomAction;
11import org.jmock.test.unit.support.AssertThat;
12
13import static org.hamcrest.Matchers.containsString;
14import static org.hamcrest.Matchers.not;
15import static org.hamcrest.StringDescription.asString;
16import static org.junit.Assert.assertThat;
17
18public class ErrorMessagesAcceptanceTests extends TestCase {
19    Mockery context = new Mockery();
20    
21    MockedType mock = context.mock(MockedType.class, "mock");
22    
23    public void testShowsExpectedAndCurrentNumberOfCallsInErrorMessage() {
24        context.checking(new Expectations() {{
25            exactly(1).of (mock).method1();
26            exactly(1).of (mock).method2();
27            atLeast(1).of (mock).method3();
28            allowing (mock).method4();
29        }});
30        
31        mock.method2();
32        mock.method3();
33        mock.method4();
34        
35        try {
36            context.assertIsSatisfied();
37        }
38        catch (ExpectationError e) {
39            String message = StringDescription.toString(e);
40            
41            AssertThat.stringIncludes("should include expectation that has not been invoked at all",
42                                      "method1", message);
43            AssertThat.stringIncludes("should include expectation that has been fully satisfied",
44                                      "method2", message);
45            AssertThat.stringIncludes("should include expectation that has been satisfied but can still be invoked",
46                                      "method3", message);
47            AssertThat.stringIncludes("should include expectation that is allowed",
48                                      "method4", message);     
49        }
50    }
51
52    // See issue JMOCK-132
53    public void testErrorMessageIncludesNotInvokedInsteadOfInvokedExactly0Times() {
54        context.checking(new Expectations() {{
55            exactly(1).of (mock).method1();
56        }});
57        
58        try {
59            context.assertIsSatisfied();
60        }
61        catch (ExpectationError e) {
62            String message = StringDescription.toString(e);
63            
64            AssertThat.stringIncludes("should include 'never invoked'", 
65                                      "never invoked", message);
66        }
67    }
68    
69    // See issue JMOCK-153
70    public void testErrorMessageIncludesOnceInsteadOfExactly1Time() {
71        context.checking(new Expectations() {{
72            exactly(1).of (mock).method1();
73        }});
74        
75        try {
76            context.assertIsSatisfied();
77        }
78        catch (ExpectationError e) {
79            String message = StringDescription.toString(e);
80            
81            AssertThat.stringIncludes("should include 'once'", 
82                                      "once", message);
83        }
84    }
85    
86    // See issue JMOCK-190
87    public void testCannotExpectToString() {
88        try {
89            context.checking(new Expectations() {{
90                allowing(mock).toString();
91            }});
92            fail("should have thrown IllegalArgumentException");
93        }
94        catch (IllegalArgumentException expected) {} 
95    }
96    
97    // See issue JMOCK-190
98    public void testCannotExpectEquals() {
99        try {
100            context.checking(new Expectations() {{
101                allowing(mock).equals("any object");
102            }});
103            fail("should have thrown IllegalArgumentException");
104        }
105        catch (IllegalArgumentException expected) {}
106    }
107    
108    // See issue JMOCK-190
109    public void testCannotExpectHashCode() {
110        try {
111            context.checking(new Expectations() {{
112                allowing(mock).hashCode();
113            }});
114            fail("should have thrown IllegalArgumentException");
115        }
116        catch (IllegalArgumentException expected) {}
117    }
118    
119    public interface TypeThatMakesFinalizePublic {
120        public void finalize();
121    }
122    
123    // See issue JMOCK-190
124    public void testCannotExpectFinalize() {
125        final TypeThatMakesFinalizePublic mockWithFinalize = context.mock(TypeThatMakesFinalizePublic.class, "mockWithFinalize");
126        
127        try {
128            context.checking(new Expectations() {{
129                allowing(mockWithFinalize).finalize();
130            }});
131            fail("should have thrown IllegalArgumentException");
132        }
133        catch (IllegalArgumentException expected) {}
134    }
135    
136    // See issue JMOCK-167
137    public void testDoesNotDescribeReturnValueForMethodsThatAreKnownToBeVoid() {
138        context.checking(new Expectations() {{
139            oneOf (mock).doSomething();
140        }});
141        
142        assertThat(asString(context),
143                   not(containsString("returns a default value")));
144    }
145
146    public void testMismatchDescription() {
147        context.checking(new Expectations() {{
148            oneOf (mock).doSomethingWith("foo");
149            oneOf (mock).doSomethingWith("x", "y"); will(doSomethingDescribedAs("ACTION"));
150        }});
151
152        try {
153            mock.doSomethingWith("X", "Y");
154        } catch (ExpectationError e) {
155            String failure = asString(e);
156            
157            Integer actionIndex = failure.indexOf("ACTION");
158            Integer parameterMismatchIndex = failure.indexOf("parameter 0 did not match");
159
160            assertTrue("action should come before parameter mismatches in description",
161                      actionIndex < parameterMismatchIndex);
162        }
163    }
164
165    public Action doSomethingDescribedAs(String name) {
166        return new CustomAction(name) {
167            public Object invoke(Invocation invocation) throws Throwable {
168                return null;
169            }
170        };
171    }
172}
173
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)