How to use ExpectationsCreator class of org.jmock package

Best Jmock-library code snippet using org.jmock.ExpectationsCreator

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.util.logging.Logger;
7
8import org.objectweb.asm.AnnotationVisitor;
9import org.objectweb.asm.ClassReader;
10import org.objectweb.asm.ClassWriter;
11import org.objectweb.asm.FieldVisitor;
12import org.objectweb.asm.MethodVisitor;
13import org.objectweb.asm.Opcodes;
14
15public class ExpectationsCreator implements Opcodes {
16
17    private static final Logger LOG = Logger.getLogger(ExpectationsCreator.class.getSimpleName());
18    
19    public static void main(String... argv) throws IOException, Exception {
20        
21        LOG.info("Expections Old School method injector running");
22        LOG.info("Running in " + new java.io.File( "." ).getCanonicalPath());
23        // Class klass = org.jmock.Expectations.class;
24        String className = "org.jmock.Expectations";
25        // String className = "java.lang.Runnable";
26        String classPath = className.replace('.', '/') + ".class";
27
28        InputStream stream = ClassLoader.getSystemResourceAsStream(classPath);
29        byte[] dump = dump(stream);
30
31        FileOutputStream output = new FileOutputStream("target/classes/"
32                + classPath);
33        output.write(dump);
34        output.close();
35    }
36
37    public static byte[] dump(InputStream stream) throws Exception {
38
39        ClassReader classReader = new ClassReader(stream);
40        ClassWriter cw = new ClassWriter(classReader, 0);
41        FieldVisitor fv;
42        MethodVisitor mv;
43        AnnotationVisitor av0;
44
45        // 1.5 to allow our generic method overloading
46        cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "org/jmock/Expectations", null,
47                "org/jmock/AbstractExpectations", null);
48
49        {
50            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
51            mv.visitCode();
52            mv.visitVarInsn(ALOAD, 0);
53            mv.visitMethodInsn(INVOKESPECIAL, "org/jmock/AbstractExpectations",
54                    "<init>", "()V");
55            mv.visitInsn(RETURN);
56            mv.visitMaxs(1, 1);
57            mv.visitEnd();
58        }
59
60        {
61            mv = cw.visitMethod(ACC_PUBLIC, "with",
62                    "(Lorg/hamcrest/Matcher;)Z",
63                    "(Lorg/hamcrest/Matcher<Ljava/lang/Boolean;>;)Z", null);
64            mv.visitCode();
65            mv.visitVarInsn(ALOAD, 0);
66            mv.visitVarInsn(ALOAD, 1);
67            mv.visitMethodInsn(INVOKESPECIAL, "org/jmock/Expectations",
68                    "addParameterMatcher", "(Lorg/hamcrest/Matcher;)V");
69            mv.visitInsn(ICONST_0);
70            mv.visitInsn(IRETURN);
71            mv.visitMaxs(2, 2);
72            mv.visitEnd();
73        }
74        {
75            mv = cw.visitMethod(ACC_PUBLIC, "with",
76                    "(Lorg/hamcrest/Matcher;)B",
77                    "(Lorg/hamcrest/Matcher<Ljava/lang/Byte;>;)B", null);
78            mv.visitCode();
79            mv.visitVarInsn(ALOAD, 0);
80            mv.visitVarInsn(ALOAD, 1);
81            mv.visitMethodInsn(INVOKESPECIAL, "org/jmock/Expectations",
82                    "addParameterMatcher", "(Lorg/hamcrest/Matcher;)V");
83            mv.visitInsn(ICONST_0);
84            mv.visitInsn(IRETURN);
85            mv.visitMaxs(2, 2);
86            mv.visitEnd();
87        }
88        {
89            mv = cw.visitMethod(ACC_PUBLIC, "with",
90                    "(Lorg/hamcrest/Matcher;)S",
91                    "(Lorg/hamcrest/Matcher<Ljava/lang/Short;>;)S", null);
92            mv.visitCode();
93            mv.visitVarInsn(ALOAD, 0);
94            mv.visitVarInsn(ALOAD, 1);
95            mv.visitMethodInsn(INVOKESPECIAL, "org/jmock/Expectations",
96                    "addParameterMatcher", "(Lorg/hamcrest/Matcher;)V");
97            mv.visitInsn(ICONST_0);
98            mv.visitInsn(IRETURN);
99            mv.visitMaxs(2, 2);
100            mv.visitEnd();
101        }
102        {
103            mv = cw.visitMethod(ACC_PUBLIC, "with",
104                    "(Lorg/hamcrest/Matcher;)C",
105                    "(Lorg/hamcrest/Matcher<Ljava/lang/Character;>;)C", null);
106            mv.visitCode();
107            mv.visitVarInsn(ALOAD, 0);
108            mv.visitVarInsn(ALOAD, 1);
109            mv.visitMethodInsn(INVOKESPECIAL, "org/jmock/Expectations",
110                    "addParameterMatcher", "(Lorg/hamcrest/Matcher;)V");
111            mv.visitInsn(ICONST_0);
112            mv.visitInsn(IRETURN);
113            mv.visitMaxs(2, 2);
114            mv.visitEnd();
115        }
116        {
117            mv = cw.visitMethod(ACC_PUBLIC, "with",
118                    "(Lorg/hamcrest/Matcher;)I",
119                    "(Lorg/hamcrest/Matcher<Ljava/lang/Integer;>;)I", null);
120            mv.visitCode();
121            mv.visitVarInsn(ALOAD, 0);
122            mv.visitVarInsn(ALOAD, 1);
123            mv.visitMethodInsn(INVOKESPECIAL, "org/jmock/Expectations",
124                    "addParameterMatcher", "(Lorg/hamcrest/Matcher;)V");
125            mv.visitInsn(ICONST_0);
126            mv.visitInsn(IRETURN);
127            mv.visitMaxs(2, 2);
128            mv.visitEnd();
129        }
130        {
131            mv = cw.visitMethod(ACC_PUBLIC, "with",
132                    "(Lorg/hamcrest/Matcher;)J",
133                    "(Lorg/hamcrest/Matcher<Ljava/lang/Long;>;)J", null);
134            mv.visitCode();
135            mv.visitVarInsn(ALOAD, 0);
136            mv.visitVarInsn(ALOAD, 1);
137            mv.visitMethodInsn(INVOKESPECIAL, "org/jmock/Expectations",
138                    "addParameterMatcher", "(Lorg/hamcrest/Matcher;)V");
139            mv.visitInsn(LCONST_0);
140            mv.visitInsn(LRETURN);
141            mv.visitMaxs(2, 2);
142            mv.visitEnd();
143        }
144        {
145            mv = cw.visitMethod(ACC_PUBLIC, "with",
146                    "(Lorg/hamcrest/Matcher;)F",
147                    "(Lorg/hamcrest/Matcher<Ljava/lang/Float;>;)F", null);
148            mv.visitCode();
149            mv.visitVarInsn(ALOAD, 0);
150            mv.visitVarInsn(ALOAD, 1);
151            mv.visitMethodInsn(INVOKESPECIAL, "org/jmock/Expectations",
152                    "addParameterMatcher", "(Lorg/hamcrest/Matcher;)V");
153            mv.visitInsn(FCONST_0);
154            mv.visitInsn(FRETURN);
155            mv.visitMaxs(2, 2);
156            mv.visitEnd();
157        }
158        {
159            mv = cw.visitMethod(ACC_PUBLIC, "with",
160                    "(Lorg/hamcrest/Matcher;)D",
161                    "(Lorg/hamcrest/Matcher<Ljava/lang/Double;>;)D", null);
162            mv.visitCode();
163            mv.visitVarInsn(ALOAD, 0);
164            mv.visitVarInsn(ALOAD, 1);
165            mv.visitMethodInsn(INVOKESPECIAL, "org/jmock/Expectations",
166                    "addParameterMatcher", "(Lorg/hamcrest/Matcher;)V");
167            mv.visitInsn(DCONST_0);
168            mv.visitInsn(DRETURN);
169            mv.visitMaxs(2, 2);
170            mv.visitEnd();
171        }
172
173        return cw.toByteArray();
174    }
175}
176
Full Screen
copy
1package org.jmock.test.acceptance;
2
3import org.jmock.Expectations;
4import org.jmock.Mockery;
5import org.jmock.api.ExpectationError;
6import org.junit.Test;
7
8public class PrimitiveParameterTypesAcceptanceTests {
9    public interface MethodsWithPrimitiveTypes {
10        void withBoolean(boolean b);
11        void withByte(byte b);
12        void withShort(short s);
13        void withInt(int i);
14        void withLong(long l);
15        void withFloat(float f);
16        void withDouble(double d);
17    }
18    
19    public final Mockery context = new Mockery();
20    private final MethodsWithPrimitiveTypes mock = context.mock(MethodsWithPrimitiveTypes.class, "mock");
21
22    @Test
23    public void canSetExpectationsWithMatchersForMethodsWithArgumentsOfPrimitiveTypes() {
24        context.checking(new Expectations() {{
25            exactly(1).of (mock).withBoolean(with.booleanIs(equal(true)));
26            exactly(1).of (mock).withByte(with.byteIs(equal((byte)10)));
27            exactly(1).of (mock).withShort(with.shortIs(equal((short) 10)));
28            exactly(1).of (mock).withInt(with.intIs(equal(10)));
29            exactly(1).of (mock).withLong(with.longIs(equal(10L)));
30            exactly(1).of (mock).withFloat(with.floatIs(equal(10.0f)));
31            exactly(1).of (mock).withDouble(with.doubleIs(equal(10.0)));
32        }});
33        
34        mock.withBoolean(true);
35        mock.withByte((byte)10);
36        mock.withShort((short)10);
37        mock.withInt(10);
38        mock.withLong(10L);
39        mock.withFloat(10.0f);
40        mock.withDouble(10.0);
41        
42        context.assertIsSatisfied();
43    }
44
45    @Test
46    public void canSetExpectationsWithLiteralsForMethodsWithArgumentsOfPrimitiveTypes() {
47        context.checking(new Expectations() {{
48            exactly(1).of (mock).withBoolean(true);
49            exactly(1).of (mock).withByte((byte)10);
50            exactly(1).of (mock).withShort((short)10);
51            exactly(1).of (mock).withInt(10);
52            exactly(1).of (mock).withLong(10L);
53            exactly(1).of (mock).withFloat(10.0f);
54            exactly(1).of (mock).withDouble(10.0);
55        }});
56        
57        mock.withBoolean(true);
58        mock.withByte((byte)10);
59        mock.withShort((short)10);
60        mock.withInt(10);
61        mock.withLong(10L);
62        mock.withFloat(10.0f);
63        mock.withDouble(10.0);
64
65        context.assertIsSatisfied();
66    }
67    
68    /**
69     * Will fail unless ExpectationsCreator has added generic overloaded byte code
70     */
71    @Test
72    public void testNonNullNativeIgnoringDocumentationParameterMatcher() {
73        context.checking(new Expectations() {{
74            exactly(1).of (mock).withBoolean(with(any(Boolean.class)));
75            exactly(1).of (mock).withByte(with(any(Byte.class)));
76            exactly(1).of (mock).withShort(with(any(Short.class)));
77            exactly(1).of (mock).withInt(with(any(Integer.class)));
78            exactly(1).of (mock).withLong(with(any(Long.class)));
79            exactly(1).of (mock).withFloat(with(any(Float.class)));
80            exactly(1).of (mock).withDouble(with(any(Double.class)));
81        }});
82        
83        mock.withBoolean(true);
84        mock.withByte((byte)10);
85        mock.withShort((short)10);
86        mock.withInt(10);
87        mock.withLong(10L);
88        mock.withFloat(10.0f);
89        mock.withDouble(10.0);
90
91        context.assertIsSatisfied();
92
93    }
94
95
96}
97
Full Screen
copy
1package org.jmock.test.acceptance;
2
3import org.jmock.Expectations;
4import org.jmock.Mockery;
5import org.junit.Test;
6
7public class PrimitiveParameterTypesAcceptanceTests {
8    public interface MethodsWithPrimitiveTypes {
9        void withBoolean(boolean b);
10        void withByte(byte b);
11        void withShort(short s);
12        void withInt(int i);
13        void withLong(long l);
14        void withFloat(float f);
15        void withDouble(double d);
16    }
17
18    public final Mockery context = new Mockery();
19    private final MethodsWithPrimitiveTypes mock = context.mock(MethodsWithPrimitiveTypes.class, "mock");
20
21    @Test
22    public void canSetExpectationsWithMatchersForMethodsWithArgumentsOfPrimitiveTypes() {
23        context.checking(new Expectations() {{
24            exactly(1).of (mock).withBoolean(with.booleanIs(equal(true)));
25            exactly(1).of (mock).withByte(with.byteIs(equal((byte)10)));
26            exactly(1).of (mock).withShort(with.shortIs(equal((short) 10)));
27            exactly(1).of (mock).withInt(with.intIs(equal(10)));
28            exactly(1).of (mock).withLong(with.longIs(equal(10L)));
29            exactly(1).of (mock).withFloat(with.floatIs(equal(10.0f)));
30            exactly(1).of (mock).withDouble(with.doubleIs(equal(10.0)));
31        }});
32        
33        mock.withBoolean(true);
34        mock.withByte((byte)10);
35        mock.withShort((short)10);
36        mock.withInt(10);
37        mock.withLong(10L);
38        mock.withFloat(10.0f);
39        mock.withDouble(10.0);
40        
41        context.assertIsSatisfied();
42    }
43
44    @Test
45    public void canSetExpectationsWithLiteralsForMethodsWithArgumentsOfPrimitiveTypes() {
46        context.checking(new Expectations() {{
47            exactly(1).of (mock).withBoolean(true);
48            exactly(1).of (mock).withByte((byte)10);
49            exactly(1).of (mock).withShort((short)10);
50            exactly(1).of (mock).withInt(10);
51            exactly(1).of (mock).withLong(10L);
52            exactly(1).of (mock).withFloat(10.0f);
53            exactly(1).of (mock).withDouble(10.0);
54        }});
55        
56        mock.withBoolean(true);
57        mock.withByte((byte)10);
58        mock.withShort((short)10);
59        mock.withInt(10);
60        mock.withLong(10L);
61        mock.withFloat(10.0f);
62        mock.withDouble(10.0);
63
64        context.assertIsSatisfied();
65    }
66    
67    /**
68     * Will fail unless ExpectationsCreator has added generic overloaded byte code
69     */
70    @Test
71    public void testNonNullNativeIgnoringDocumentationParameterMatcher() {
72        context.checking(new Expectations() {{
73            exactly(1).of(mock).withBoolean(with.booleanIs(anything()));
74            exactly(1).of(mock).withByte(with.byteIs(anything()));
75            exactly(1).of(mock).withShort(with.shortIs(anything()));
76            exactly(1).of(mock).withInt(with.intIs(anything()));
77            exactly(1).of(mock).withLong(with.longIs(anything()));
78            exactly(1).of(mock).withFloat(with.floatIs(anything()));
79            exactly(1).of(mock).withDouble(with.doubleIs(anything()));
80        }});
81
82        mock.withBoolean(true);
83        mock.withByte((byte)10);
84        mock.withShort((short)10);
85        mock.withInt(10);
86        mock.withLong(10L);
87        mock.withFloat(10.0f);
88        mock.withDouble(10.0);
89
90        context.assertIsSatisfied();
91    }
92}
93
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

...Most popular Stackoverflow questions on ExpectationsCreator

    No relevant questions found for this class 😞

Most used methods in ExpectationsCreator

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)