How to use method of org.jmock.lib.legacy.ClassImposteriser class

Best Jmock-library code snippet using org.jmock.lib.legacy.ClassImposteriser.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jtester.unitils.jmock;
2
3import java.lang.reflect.InvocationTargetException;
4import java.lang.reflect.Method;
5
6import org.jmock.api.Imposteriser;
7import org.jmock.api.Invocation;
8import org.jmock.api.Invokable;
9import org.jmock.lib.legacy.ClassImposteriser;
10
11public class BeanProxy implements Invokable {
12	private String name;
13
14	private Class<?> type;
15
16	protected BeanProxy(final String name, final Class<?> type) {
17		this.name = name;
18		this.type = type;
19	}
20
21	public Object invoke(Invocation invocation) throws Throwable {
22		Object mock = MockBeanRegister.getBean(name, type);
23		Method method = invocation.getInvokedMethod();
24		try {
25			return method.invoke(mock, invocation.getParametersAsArray());
26		} catch (Throwable e) {
27			if (e instanceof InvocationTargetException) {
28				throw ((InvocationTargetException) e).getTargetException();
29			} else {
30				throw e;
31			}
32		}
33	}
34
35	private static Imposteriser imposteriser = ClassImposteriser.INSTANCE;
36
37	@SuppressWarnings("unchecked")
38	public static <T> T proxy(final String name, final Class<?> type) {
39		return (T) imposteriser.imposterise(new BeanProxy(name, type), type);
40	}
41}
42
Full Screen
copy
1package com.oneeyedmen.okeydoke.internal;
2
3import org.jmock.api.Invocation;
4import org.jmock.api.Invokable;
5import org.jmock.lib.legacy.ClassImposteriser;
6
7import java.lang.reflect.InvocationHandler;
8
9public class Fred {
10
11    private static final org.jmock.api.Imposteriser IMPOSTERISER = loadImposteriser();
12
13    private static org.jmock.api.Imposteriser loadImposteriser() {
14        try {
15            return ClassImposteriser.INSTANCE;
16        } catch (Throwable t) {
17            throw new ExceptionInInitializerError("You need JMock and JMock-Legacy in your classpath to do this");
18        }
19    }
20
21    public static <T> T newProxyInstance(Class<T> type, final InvocationHandler handler)
22    {
23        Invokable invokable = new Invokable() {
24            @Override
25            public Object invoke(Invocation invocation) throws Throwable {
26                return handler.invoke(null, invocation.getInvokedMethod(), invocation.getParametersAsArray());
27            }
28        };
29        return IMPOSTERISER.imposterise(invokable, type);
30    }
31}
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)