How to use method of org.jmock.lib.JavaReflectionImposteriser class

Best Jmock-library code snippet using org.jmock.lib.JavaReflectionImposteriser.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.lib;
2
3import java.lang.reflect.InvocationHandler;
4import java.lang.reflect.Method;
5import java.lang.reflect.Proxy;
6
7import org.jmock.api.Imposteriser;
8import org.jmock.api.Invocation;
9import org.jmock.api.Invokable;
10import org.jmock.internal.SearchingClassLoader;
11
12/**
13 * An {@link org.jmock.api.Imposteriser} that uses the
14 * {@link java.lang.reflect.Proxy} class of the Java Reflection API.
15 * 
16 * @author npryce
17 *
18 */
19public class JavaReflectionImposteriser implements Imposteriser {
20    public static final Imposteriser INSTANCE = new JavaReflectionImposteriser();
21    
22    public boolean canImposterise(Class<?> type) {
23        return type.isInterface();
24    }
25    
26    @SuppressWarnings("unchecked")
27    public <T> T imposterise(final Invokable mockObject, Class<T> mockedType, Class<?>... ancilliaryTypes) {
28        final Class<?>[] proxiedClasses = prepend(mockedType, ancilliaryTypes);
29        final ClassLoader classLoader = SearchingClassLoader.combineLoadersOf(proxiedClasses);
30        
31        return (T)Proxy.newProxyInstance(classLoader, proxiedClasses, new InvocationHandler() {
32            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
33                return mockObject.invoke(new Invocation(proxy, method, args));
34            }
35        });
36    }
37    
38    private Class<?>[] prepend(Class<?> first, Class<?>... rest) {
39        Class<?>[] proxiedClasses = new Class<?>[rest.length+1];
40        
41        proxiedClasses[0] = first;
42        System.arraycopy(rest, 0, proxiedClasses, 1, rest.length);
43        
44        return proxiedClasses;
45    }
46}
47
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 used method in JavaReflectionImposteriser

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)