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

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

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