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

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

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 static java.util.Arrays.asList;
4import static org.hamcrest.StringDescription.asString;
5import static org.junit.Assert.assertThat;
6import junit.framework.TestCase;
7
8import org.hamcrest.Matcher;
9import org.hamcrest.text.StringContainsInOrder;
10import org.jmock.Expectations;
11import org.jmock.Mockery;
12import org.jmock.api.ExpectationError;
13
14public class RecordingAcceptanceTests extends TestCase {
15    Mockery context = new Mockery();
16    MockedType mock = context.mock(MockedType.class, "mock");
17    
18    public void testRecordsActualInvocations() {
19        context.checking(new Expectations() {{
20            allowing (same(mock));
21        }});
22        
23        mock.doSomething();
24        mock.doSomethingWith("foo");
25        mock.doSomethingWith("x", "y");
26        
27        assertThat(asString(context), containsInOrder(
28            "what happened before this:",
29            "mock.doSomething()",
30            "mock.doSomethingWith(\"foo\")",
31            "mock.doSomethingWith(\"x\", \"y\")"));
32    }
33
34    static class ExampleException extends RuntimeException {}
35    
36    public void testRecordsInvocationsThatThrowExceptions() {
37        context.checking(new Expectations() {{
38            allowing (mock).doSomething(); will(throwException(new ExampleException()));
39        }});
40        
41        try {
42            mock.doSomething();
43            fail("no exception thrown");
44        }
45        catch (ExampleException expected) {}
46        
47        assertThat(asString(context), containsInOrder(
48            "what happened before this:",
49            "mock.doSomething()"));
50    }
51
52    public void testDoesNotRecordUnexpectedInvocations() {
53        context.checking(new Expectations() {{
54            allowing (mock).doSomethingWith("foo");
55        }});
56        
57        try {
58            mock.doSomethingWith("bar");
59        }
60        catch (ExpectationError expected) {}
61        
62        assertThat(asString(context), containsInOrder(
63            "what happened before this:", 
64            "nothing"));
65    }
66    
67    public void testReportsRecordedInvocationsWhenUnexpectedInvocationReceived() {
68        context.checking(new Expectations() {{
69            oneOf (mock).doSomethingWith("x");
70            oneOf (mock).doSomethingWith("y");
71        }});
72        
73        mock.doSomethingWith("y");
74        
75        try {
76            mock.doSomethingWith("z");
77            fail("should have reported unexpected invocation");
78        }
79        catch (ExpectationError e) {
80            assertThat(asString(e), containsInOrder(
81                "what happened before this:",
82                "mock.doSomethingWith(\"y\")"
83            ));
84        }
85    }
86    
87    public void testReportsRecordedInvocationsWhenNotSatisfied() {
88        context.checking(new Expectations() {{
89            oneOf (mock).doSomethingWith("x");
90            oneOf (mock).doSomethingWith("y");
91        }});
92        
93        mock.doSomethingWith("y");
94        
95        try {
96            context.assertIsSatisfied();
97            fail("should not be satisfied");
98        }
99        catch (ExpectationError e) {
100            assertThat(asString(e), containsInOrder(
101                "what happened before this:",
102                "mock.doSomethingWith(\"y\")"
103            ));
104        }
105    }
106    
107    private Matcher<? super String> containsInOrder(String... strings) {
108        return new StringContainsInOrder(asList(strings));
109    }
110}
111
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

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
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)