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

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

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;
4
5import org.jmock.Expectations;
6import org.jmock.Mockery;
7import org.jmock.api.ExpectationError;
8
9public class ParameterMatchingAcceptanceTests extends TestCase {
10    public interface AnInterface {
11        void doSomethingWith(String s);
12        void doSomethingWithBoth(String s1, String s2);
13        
14        void doSomethingWithBoth(boolean i1, boolean i2);
15        void doSomethingWithBoth(byte i1, byte i2);
16        void doSomethingWithBoth(short i1, short i2);
17        void doSomethingWithBoth(char c1, char c2);
18        void doSomethingWithBoth(int i1, int i2);
19        void doSomethingWithBoth(long i1, long i2);
20        void doSomethingWithBoth(float i1, float i2);
21        void doSomethingWithBoth(double i1, double i2);
22    }
23    
24    Mockery context = new Mockery();
25    AnInterface mock = context.mock(AnInterface.class, "mock");
26    
27    public void testMatchesParameterValues() {
28        context.checking(new Expectations() {{
29            exactly(1).of (mock).doSomethingWith(with(equal("hello")));
30            exactly(1).of (mock).doSomethingWith(with(equal("goodbye")));
31        }});
32        
33        mock.doSomethingWith("hello");
34        mock.doSomethingWith("goodbye");
35        
36        context.assertIsSatisfied();
37    }
38    
39    public void testDoesNotAllowUnexpectedParameterValues() {
40        context.checking(new Expectations() {{
41            exactly(1).of (mock).doSomethingWith(with(equal("hello")));
42            exactly(1).of (mock).doSomethingWith(with(equal("goodbye")));
43        }});
44        
45        try {
46            mock.doSomethingWith("hello");
47            mock.doSomethingWith("Goodbye");
48            fail("should have thrown ExpectationError");
49        }
50        catch (ExpectationError expected) {}
51    }
52    
53    public void testAllOrNoneOfTheParametersMustBeSpecifiedByMatchers() {
54        try {
55            context.checking(new Expectations() {{
56                exactly(1).of (mock).doSomethingWithBoth(with(equal("a-matcher")), "not-a-matcher");
57            }});
58        }
59        catch (IllegalArgumentException expected) {
60        }
61    }
62    
63    // Test to show that issue JMOCK-160 is spurious
64    public void testNotAllExpectationsOfSameMockMustUseMatchers() {
65        context.checking(new Expectations() {{
66            exactly(1).of (mock).doSomethingWithBoth(with(equal("x")), with(equal("y")));
67            exactly(1).of (mock).doSomethingWith("z");
68        }});
69        
70        mock.doSomethingWithBoth("x", "y");
71        mock.doSomethingWith("z");
72        
73        context.assertIsSatisfied();
74    }
75    
76    // See issue JMOCK-161
77    public void testCanPassLiteralValuesToWithMethodToMeanEqualTo() {
78        context.checking(new Expectations() {{
79            exactly(2).of (mock).doSomethingWithBoth(with(any(String.class)), with("y"));
80        }});
81        
82        mock.doSomethingWithBoth("x", "y");
83        mock.doSomethingWithBoth("z", "y");
84        
85        context.assertIsSatisfied();
86    }
87    
88    public void testCanPassLiteralPrimitiveValuesToWithMethodToMeanEqualTo() {
89        context.checking(new Expectations() {{
90            exactly(2).of (mock).doSomethingWithBoth(with.booleanIs(any(boolean.class)), with(true));
91            exactly(2).of (mock).doSomethingWithBoth(with.byteIs(any(byte.class)), with((byte)1));
92            exactly(2).of (mock).doSomethingWithBoth(with.shortIs(any(short.class)), with((short)2));
93            exactly(2).of (mock).doSomethingWithBoth(with.charIs(any(char.class)), with('x'));
94            exactly(2).of (mock).doSomethingWithBoth(with.intIs(any(int.class)), with(3));
95            exactly(2).of (mock).doSomethingWithBoth(with.longIs(any(long.class)), with(4L));
96            exactly(2).of (mock).doSomethingWithBoth(with.floatIs(any(float.class)), with(5.0f));
97            exactly(2).of (mock).doSomethingWithBoth(with.doubleIs(any(double.class)), with(6.0));
98        }});
99        
100        mock.doSomethingWithBoth(true, true);
101        mock.doSomethingWithBoth(false, true);
102        
103        mock.doSomethingWithBoth((byte)1, (byte)1);
104        mock.doSomethingWithBoth((byte)2, (byte)1);
105        
106        mock.doSomethingWithBoth((short)1, (short)2);
107        mock.doSomethingWithBoth((short)2, (short)2);
108        
109        mock.doSomethingWithBoth('1', 'x');
110        mock.doSomethingWithBoth('2', 'x');
111        
112        mock.doSomethingWithBoth(1, 3);
113        mock.doSomethingWithBoth(2, 3);
114        
115        mock.doSomethingWithBoth(1L, 4L);
116        mock.doSomethingWithBoth(2L, 4L);
117        
118        mock.doSomethingWithBoth(1.0f, 5.0f);
119        mock.doSomethingWithBoth(2.0f, 5.0f);
120        
121        mock.doSomethingWithBoth(1.0, 6.0);
122        mock.doSomethingWithBoth(2.0, 6.0);
123        
124        context.assertIsSatisfied();
125    }
126    
127    // Checking that you can do with(any(...)) with primitive types, as asked on the mailing list
128    public void testSpecifyingAnyValueOfPrimitiveType() {
129        context.checking(new Expectations() {{
130            allowing (mock).doSomethingWithBoth(with.booleanIs(any(boolean.class)), with.booleanIs(any(boolean.class)));
131        }});
132        
133        mock.doSomethingWithBoth(true, true);
134        mock.doSomethingWithBoth(true, false);
135        mock.doSomethingWithBoth(false, true);
136        mock.doSomethingWithBoth(false, false);
137    }
138}
139
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)