How to use imposterise method of org.jmock.imposters.ByteBuddyClassImposteriser class

Best Jmock-library code snippet using org.jmock.imposters.ByteBuddyClassImposteriser.imposterise

Source:BaseCachedJMockTestCase.java Github

copy

Full Screen

...26abstract public class BaseCachedJMockTestCase extends TestCase {27 protected final Mockery context;28 {29 // use an initializer rather than setUp so forgetting to30 // call super.setUp won't use the wrong imposteriser31 context = new Mockery();32 context.setThreadingPolicy(new Synchroniser());33 context.setImposteriser(CachingImposteriser.INSTANCE);34 new Mockomatic(context).fillIn(this);35 }36 public <T> T mock(Class<T> tClass) {37 return context.mock(tClass);38 }39 public <T> T mock(Class<T> tClass, String s) {40 return context.mock(tClass, s);41 }42 public States states(String s) {43 return context.states(s);44 }45 public Sequence sequence(String s) {46 return context.sequence(s);47 }48 public void checking(ExpectationBuilder expectationBuilder) {49 context.checking(expectationBuilder);50 }51 public void assertIsSatisfied() {52 context.assertIsSatisfied();53 }54 @Override55 protected void tearDown() throws Exception {56 context.assertIsSatisfied();57 super.tearDown();58 }59 public static class Expectations extends org.jmock.Expectations {60 public static <T> Matcher<T> some(Class<T> type) {61 return CoreMatchers.instanceOf(type);62 }63 public static <T> Matcher<T> any(Class<T> type) {64 return CoreMatchers.anyOf(CoreMatchers.instanceOf(type), CoreMatchers.nullValue(type));65 }66 private static AtomicInteger willDoCounter = new AtomicInteger(0);67 public void willDo(final Function.Nullary<Object> proc) {68 this.currentBuilder().setAction(run(proc));69 }70 public static CustomAction run(final Function.Nullary<Object> proc) {71 return new CustomAction("willDo_" + willDoCounter.incrementAndGet()) {72 @Override73 public Object invoke(Invocation invocation) throws Throwable {74 return proc.call();75 }76 };77 }78 public void willDo(final Function.Unary<Object, Invocation> proc) {79 this.currentBuilder().setAction(run(proc));80 }81 public static CustomAction run(final Function.Unary<Object, Invocation> proc) {82 return new CustomAction("willDo_" + willDoCounter.incrementAndGet()) {83 @Override84 public Object invoke(Invocation invocation) throws Throwable {85 return proc.call(invocation);86 }87 };88 }89 }90 // ----------------------------------------------------------------91 public static class CachingImposteriser implements Imposteriser {92 public static final BaseCachedJMockTestCase.CachingImposteriser INSTANCE = new CachingImposteriser();93 private final static Class[] CONSTRUCTOR_PARAMS = {InvocationHandler.class};94 private static Map<ProxyInfo, Function.Unary<?, Invokable>> proxyInfoToConstructorMap = new HashMap<>();95 // ----------------------------------------------------------------96 @Override // from Imposteriser97 public boolean canImposterise(Class<?> type) {98 return ByteBuddyClassImposteriser.INSTANCE.canImposterise(type);99 }100 // ----------------------------------------------------------------101 @Override // from Imposteriser102 public <T> T imposterise(final Invokable mockObject, Class<T> mockedType, Class<?>... ancillaryTypes) {103 ProxyInfo proxyInfo = new ProxyInfo(mockedType, ancillaryTypes);104 Function.Unary<?, Invokable> constructor = proxyInfoToConstructorMap.get(proxyInfo);105 if (null == constructor) {106 constructor = createConstructor(proxyInfo);107 proxyInfoToConstructorMap.put(proxyInfo, constructor);108 }109 // noinspection unchecked110 return (T) constructor.call(mockObject);111 }112 // ----------------------------------------------------------------113 private Function.Unary<?, Invokable> createConstructor(ProxyInfo proxyInfo) {114 if (proxyInfo.mockedType.isInterface()) {115 return createInterfaceConstructor(proxyInfo);116 } else {117 return createClassConstructor(proxyInfo);118 }119 }120 // ----------------------------------------------------------------121 /** Based on {@link org.jmock.lib.JavaReflectionImposteriser}. */122 private Function.Unary<?, Invokable> createInterfaceConstructor(ProxyInfo proxyInfo) {123 ClassLoader proxyClassLoader = BaseCachedJMockTestCase.class.getClassLoader();124 Class proxyClass = Proxy.getProxyClass(proxyClassLoader, proxyInfo.proxiedClasses);125 final Constructor constructor;126 try {127 constructor = proxyClass.getConstructor(CONSTRUCTOR_PARAMS);128 } catch (NoSuchMethodException e) {129 throw Assert.exceptionNeverCaught(e);130 }131 return new Function.Unary<Object, Invokable>() {132 @Override133 public Object call(final Invokable invokable) {134 try {135 return constructor.newInstance(new InvocationHandler() {136 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {137 return invokable.invoke(new Invocation(proxy, method, args));138 }139 });140 } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {141 throw Assert.exceptionNeverCaught(e);142 }143 }144 };145 }146 // ----------------------------------------------------------------147 /** Based on {@link ByteBuddyClassImposteriser}. */148 private Function.Unary<?, Invokable> createClassConstructor(final ProxyInfo proxyInfo) {149 return imposter -> ByteBuddyClassImposteriser.INSTANCE.imposterise(imposter, proxyInfo.mockedType,150 proxyInfo.ancillaryTypes);151 }152 }153 // ----------------------------------------------------------------154 private static class ProxyInfo {155 public Class[] proxiedClasses;156 public Class mockedType;157 public Class[] ancillaryTypes;158 // ----------------------------------------------------------------159 public ProxyInfo(Class<?> mockedType, Class<?>... ancillaryTypes) {160 this.mockedType = mockedType;161 this.ancillaryTypes = ancillaryTypes;162 proxiedClasses = new Class<?>[ancillaryTypes.length + 1];163 proxiedClasses[0] = mockedType;...

Full Screen

Full Screen

Source:ByteBuddyClassImposteriser.java Github

copy

Full Screen

...29import net.bytebuddy.implementation.bind.annotation.RuntimeType;30import net.bytebuddy.implementation.bind.annotation.This;31import net.bytebuddy.matcher.ElementMatchers;32/**33 * This class lets you imposterise abstract and concrete classes34 * <em>without</em> calling the constructors of the mocked class.35 * 36 * @author olibye37 */38public class ByteBuddyClassImposteriser implements Imposteriser {39 public static final Imposteriser INSTANCE = new ByteBuddyClassImposteriser();40 private static final String JMOCK_KEY = "jMock";41 private final Objenesis objenesis = new ObjenesisStd();42 private final Map<Set<Class<?>>, Class<?>> types = new ConcurrentHashMap<Set<Class<?>>, Class<?>>();43 private ByteBuddyClassImposteriser() {44 }45 public boolean canImposterise(Class<?> type) {46 return !type.isPrimitive() &&47 !Modifier.isFinal(type.getModifiers()) &&48 (type.isInterface() || !toStringMethodIsFinal(type));49 }50 @Override51 public <T> T imposterise(final Invokable mockObject, Class<T> mockedType, Class<?>... ancilliaryTypes) {52 if (!mockedType.isInterface() && toStringMethodIsFinal(mockedType)) {53 throw new IllegalArgumentException(mockedType.getName() + " has a final toString method");54 }55 try {56 setConstructorsAccessible(mockedType, true);57 return proxy(mockObject, mockedType, ancilliaryTypes);58 } finally {59 setConstructorsAccessible(mockedType, false);60 }61 }62 private boolean toStringMethodIsFinal(Class<?> type) {63 try {64 Method toString = type.getMethod("toString");65 return Modifier.isFinal(toString.getModifiers());...

Full Screen

Full Screen

Source:ClassImposteriser.java Github

copy

Full Screen

...14import java.lang.reflect.Method;15import java.lang.reflect.Modifier;16import java.util.List;17/**18 * This class lets you imposterise abstract and concrete classes 19 * <em>without</em> calling the constructors of the mocked class.20 * 21 * @author npryce22 * 23 * @deprecated Java11 support is weak, Migrate to org.jmock.lib.imposters.ByteBuddyClassImposteriser24 */25public class ClassImposteriser implements Imposteriser {26 public static final Imposteriser INSTANCE = new ClassImposteriser();27 28 private ClassImposteriser() {}29 private static final Method FINALIZE_METHOD = findFinalizeMethod();30 private static final NamingPolicy NAMING_POLICY_THAT_ALLOWS_IMPOSTERISATION_OF_CLASSES_IN_SIGNED_PACKAGES = new DefaultNamingPolicy() {31 @Override32 public String getClassName(String prefix, String source, Object key, Predicate names) {33 return "org.jmock.codegen." + super.getClassName(prefix, source, key, names);34 }35 };36 37 private static final CallbackFilter IGNORED_METHODS = new CallbackFilter() {38 public int accept(Method method) {39 if (method.isBridge())40 return 1;41 else if (method.equals(FINALIZE_METHOD))42 return 1;43 else44 return 0;45 }46 };47 48 private final Objenesis objenesis = new ObjenesisStd();49 50 public boolean canImposterise(Class<?> type) {51 return !type.isPrimitive() && 52 !Modifier.isFinal(type.getModifiers()) && 53 (type.isInterface() || !toStringMethodIsFinal(type));54 }55 56 @Override57 public <T> T imposterise(final Invokable mockObject, Class<T> mockedType, Class<?>... ancilliaryTypes) {58 if (!mockedType.isInterface() && toStringMethodIsFinal(mockedType)) {59 throw new IllegalArgumentException(mockedType.getName() + " has a final toString method");60 }61 62 try {63 setConstructorsAccessible(mockedType, true);64 return mockedType.cast(proxy(proxyClass(mockedType, ancilliaryTypes), mockObject));65 }66 finally {67 setConstructorsAccessible(mockedType, false);68 }69 }70 71 private boolean toStringMethodIsFinal(Class<?> type) {72 try {73 Method toString = type.getMethod("toString");74 return Modifier.isFinal(toString.getModifiers());75 76 }77 catch (SecurityException e) {78 throw new IllegalStateException("not allowed to reflect on toString method", e);79 }80 catch (NoSuchMethodException e) {81 throw new Error("no public toString method found", e);82 }83 }84 private void setConstructorsAccessible(Class<?> mockedType, boolean accessible) {85 for (Constructor<?> constructor : mockedType.getDeclaredConstructors()) {86 constructor.setAccessible(accessible);87 }88 }89 90 private Class<?> proxyClass(Class<?> possibleMockedType, Class<?>... ancilliaryTypes) {91 final Class<?> mockedType =92 possibleMockedType == Object.class ? ClassWithSuperclassToWorkAroundCglibBug.class : possibleMockedType;93 94 final Enhancer enhancer = new Enhancer() {95 @Override96 @SuppressWarnings("unchecked")97 protected void filterConstructors(Class sc, List constructors) {98 // Don't filter99 }100 };101 enhancer.setClassLoader(SearchingClassLoader.combineLoadersOf(mockedType, ancilliaryTypes));102 enhancer.setUseFactory(true);103 if (mockedType.isInterface()) {104 enhancer.setSuperclass(Object.class);105 enhancer.setInterfaces(prepend(mockedType, ancilliaryTypes));106 }107 else {108 enhancer.setSuperclass(mockedType);109 enhancer.setInterfaces(ancilliaryTypes);110 }111 enhancer.setCallbackTypes(new Class[]{InvocationHandler.class, NoOp.class});112 enhancer.setCallbackFilter(IGNORED_METHODS);113 if (protectedPackageNamespace(mockedType)) {114 enhancer.setNamingPolicy(NAMING_POLICY_THAT_ALLOWS_IMPOSTERISATION_OF_CLASSES_IN_SIGNED_PACKAGES);115 }116 117 try {118 return enhancer.createClass();119 }120 catch (CodeGenerationException e) {121 // Note: I've only been able to manually test this. It exists to help people writing122 // Eclipse plug-ins or using other environments that have sophisticated class loader123 // structures.124 throw new IllegalArgumentException("could not imposterise " + mockedType, e);125 }126 }127 128 private Object proxy(Class<?> proxyClass, final Invokable mockObject) {129 final Factory proxy = (Factory)objenesis.newInstance(proxyClass);130 proxy.setCallbacks(new Callback[] {131 new InvocationHandler() {132 public Object invoke(Object receiver, Method method, Object[] args) throws Throwable {133 return mockObject.invoke(new Invocation(receiver, method, args));134 }135 },136 NoOp.INSTANCE137 });138 return proxy;...

Full Screen

Full Screen

imposterise

Using AI Code Generation

copy

Full Screen

1import org.jmock.imposters.ByteBuddyClassImposteriser;2import org.jmock.imposters.ByteBuddyImposteriser;3import org.jmock.imposters.ByteBuddyInterfaceImposteriser;4import org.jmock.imposters.Imposteriser;5import org.jmock.imposters.ByteBuddyImposteriser;6import org.jmock.imposters.ByteBuddyClassImposteriser;7import org.jmock.imposters.ByteBuddyInterfaceImposteriser;8import org.jmock.imposters.Imposteriser;9public class 1 {10 public static void main(String[] args) {11 Imposteriser imposteriser = new ByteBuddyClassImposteriser();12 System.out.println("Imposteriser is: " + imposteriser);13 }14}

Full Screen

Full Screen

imposterise

Using AI Code Generation

copy

Full Screen

1import org.jmock.imposters.ByteBuddyClassImposteriser;2public class 1 {3 public static void main(String[] args) {4 ByteBuddyClassImposteriser imposteriser = new ByteBuddyClassImposteriser();5 List list = imposteriser.imposterise(new ArrayList(), List.class);6 list.add("Hello");7 System.out.println(list.get(0));8 }9}

Full Screen

Full Screen

imposterise

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.acceptance;2import org.jmock.Expectations;3import org.jmock.Mockery;4import org.jmock.api.Imposteriser;5import org.jmock.imposters.ByteBuddyClassImposteriser;6import org.jmock.test.unit.lib.legacy.ClassImposteriserTest;7import org.junit.Test;8public class ByteBuddyClassImposteriserAcceptanceTests extends ClassImposteriserTest {9 protected Imposteriser createImposteriser() {10 return new ByteBuddyClassImposteriser();11 }12}13package org.jmock.test.acceptance;14import org.jmock.Expectations;15import org.jmock.Mockery;16import org.jmock.api.Imposteriser;17import org.jmock.imposters.ByteBuddyInterfaceImposteriser;18import org.jmock.test.unit.lib.legacy.InterfaceImposteriserTest;19import org.junit.Test;20public class ByteBuddyInterfaceImposteriserAcceptanceTests extends InterfaceImposteriserTest {21 protected Imposteriser createImposteriser() {22 return new ByteBuddyInterfaceImposteriser();23 }24}25package org.jmock.test.acceptance;26import org.jmock.Expectations;27import org.jmock.Mockery;28import org.jmock.api.Imposteriser;29import org.jmock.imposters.ByteBuddyImposteriser;30import org.jmock.test.unit.lib.legacy.ImposteriserTest;31import org.junit.Test;32public class ByteBuddyImposteriserAcceptanceTests extends ImposteriserTest {33 protected Imposteriser createImposteriser() {34 return new ByteBuddyImposteriser();35 }36}37package org.jmock.test.acceptance;38import org.jmock.Expectations;39import org.jmock.Mockery;40import org.jmock.api.Imposteriser;41import org.jmock.imposters.ByteBuddyMethodImposteriser;42import org.jmock.test.unit.lib.legacy.MethodImposteriserTest;43import org.junit.Test;

Full Screen

Full Screen

imposterise

Using AI Code Generation

copy

Full Screen

1import org.jmock.imposters.ByteBuddyClassImposteriser;2import org.jmock.imposters.ByteBuddyImposteriser;3public class ByteBuddyImposteriserTest {4 public static void main(String[] args) {5 ByteBuddyImposteriser imposteriser = new ByteBuddyClassImposteriser();6 Foo foo = imposteriser.imposterise(new Foo(), Foo.class);7 foo.doSomething();8 }9}10import org.jmock.imposters.ByteBuddyInterfaceImposteriser;11import org.jmock.imposters.ByteBuddyImposteriser;12public class ByteBuddyImposteriserTest {13 public static void main(String[] args) {14 ByteBuddyImposteriser imposteriser = new ByteBuddyInterfaceImposteriser();15 Foo foo = imposteriser.imposterise(new Foo(), Foo.class);16 foo.doSomething();17 }18}

Full Screen

Full Screen

imposterise

Using AI Code Generation

copy

Full Screen

1import org.jmock.imposters.ByteBuddyClassImposteriser;2import org.jmock.imposters.ByteBuddyImposteriser;3import org.jmock.imposters.Imposteriser;4import org.jmock.imposters.ClassImposteriser;5public class TestImposterise {6 public static void main(String[] args) {7 Imposteriser imposteriser = new ByteBuddyClassImposteriser();8 Foo foo = imposteriser.imposterise(new Foo(), Foo.class);9 foo.doSomething();10 }11}

Full Screen

Full Screen

imposterise

Using AI Code Generation

copy

Full Screen

1import org.jmock.imposters.ByteBuddyClassImposteriser;2import org.jmock.imposters.ByteBuddyImposteriser;3public class 1 {4 public static void main(String[] args) {5 ByteBuddyImposteriser imposteriser = new ByteBuddyImposteriser();6 Class<?> proxyClass = imposteriser.imposterise(Runnable.class, Runnable.class, ClassImposteriser.INSTANCE);7 System.out.println(proxyClass);8 }9}10import org.jmock.imposters.ByteBuddyClassImposteriser;11import org.jmock.imposters.ByteBuddyImposteriser;12public class 2 {13 public static void main(String[] args) {14 ByteBuddyImposteriser imposteriser = new ByteBuddyImposteriser();15 Class<?> proxyClass = imposteriser.imposterise(Runnable.class, Runnable.class, ClassImposteriser.INSTANCE);16 System.out.println(proxyClass);17 }18}19 at org.jmock.imposters.ByteBuddyClassImposteriser.imposterise(ByteBuddyClassImposteriser.java:32)20 at 2.main(2.java:10)

Full Screen

Full Screen

imposterise

Using AI Code Generation

copy

Full Screen

1import org.jmock.imposters.ByteBuddyClassImposteriser;2import org.jmock.Mockery;3import org.jmock.Expectations;4public class 1 {5public static void main(String[] args) {6Mockery context = new Mockery();7context.setImposteriser(ByteBuddyClassImposteriser.INSTANCE);8final List list = context.mock(List.class);9context.checking(new Expectations() {{10oneOf (list).add("Hello");11}});12list.add("Hello");13}14}

Full Screen

Full Screen

imposterise

Using AI Code Generation

copy

Full Screen

1import org.jmock.imposters.ByteBuddyClassImposteriser;2import org.jmock.imposters.ByteBuddyClassImposteriser;3public class 1 {4 public static void main(String[] args) {5 ClassImposteriser classImposteriser = new ByteBuddyClassImposteriser();6 }7}8import org.jmock.imposters.ByteBuddyClassImposteriser;9import org.jmock.imposters.ByteBuddyClassImposteriser;10public class 2 {11 public static void main(String[] args) {12 ClassImposteriser classImposteriser = new ByteBuddyClassImposteriser();13 }14}15import org.jmock.imposters.ByteBuddyClassImposteriser;16import org.jmock.imposters.ByteBuddyClassImposteriser;17public class 3 {18 public static void main(String[] args) {19 ClassImposteriser classImposteriser = new ByteBuddyClassImposteriser();20 }21}22import org.jmock.imposters.ByteBuddyClassImposteriser;23import org.jmock.imposters.ByteBuddyClassImposteriser;24public class 4 {25 public static void main(String[] args) {26 ClassImposteriser classImposteriser = new ByteBuddyClassImposteriser();27 }28}

Full Screen

Full Screen

imposterise

Using AI Code Generation

copy

Full Screen

1package org.jmock.imposters;2import org.jmock.api.Imposteriser;3import org.jmock.imposters.ByteBuddyClassImposteriser;4{5 public static void main(String[] args)6 {7 Imposteriser imposteriser = new ByteBuddyClassImposteriser();8 A a = imposteriser.imposterise(new A(), A.class);9 a.method();10 }11}12{13 public void method()14 {15 System.out.println("Hello World");16 }17}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Jmock-library automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful