use of org.robolectric.internal.bytecode.MethodRef in project robolectric by robolectric.
the class SandboxClassLoaderTest method shouldInterceptFilteredStaticMethodInvocations.
@Test
public void shouldInterceptFilteredStaticMethodInvocations() throws Exception {
setClassLoader(new SandboxClassLoader(configureBuilder().addInterceptedMethod(new MethodRef(AClassToForget.class, "forgettableStaticMethod")).build()));
Class<?> theClass = loadClass(AClassThatRefersToAForgettableClass.class);
Object instance = theClass.newInstance();
Object output = theClass.getMethod("interactWithForgettableStaticMethod").invoke(shadow.directlyOn(instance, (Class<Object>) theClass));
assertEquals("yess? forget this: null", output);
}
use of org.robolectric.internal.bytecode.MethodRef in project robolectric by robolectric.
the class SandboxClassLoaderTest method shouldInterceptFilteredMethodInvocations.
@Test
public void shouldInterceptFilteredMethodInvocations() throws Exception {
setClassLoader(new SandboxClassLoader(configureBuilder().addInterceptedMethod(new MethodRef(AClassToForget.class, "forgettableMethod")).build()));
Class<?> theClass = loadClass(AClassThatRefersToAForgettableClass.class);
Object instance = theClass.newInstance();
Object output = theClass.getMethod("interactWithForgettableClass").invoke(shadow.directlyOn(instance, (Class<Object>) theClass));
assertEquals("null, get this!", output);
}
use of org.robolectric.internal.bytecode.MethodRef in project robolectric by robolectric.
the class SandboxClassLoaderTest method shouldPassArgumentsFromInterceptedMethods.
@Test
public void shouldPassArgumentsFromInterceptedMethods() throws Exception {
if (InvokeDynamic.ENABLED)
return;
classHandler.valueToReturnFromIntercept = 10L;
setClassLoader(new SandboxClassLoader(configureBuilder().addInterceptedMethod(new MethodRef(AClassToForget.class, "*")).build()));
Class<?> theClass = loadClass(AClassThatRefersToAForgettableClassInMethodCallsReturningPrimitive.class);
Object instance = theClass.newInstance();
shadow.directlyOn(instance, (Class<Object>) theClass, "longMethod");
assertThat(transcript).containsExactly("methodInvoked: AClassThatRefersToAForgettableClassInMethodCallsReturningPrimitive.__constructor__()", "intercept: org/robolectric/testing/AClassToForget/longReturningMethod(Ljava/lang/String;IJ)J with params (str str, 123 123, 456 456)");
}
use of org.robolectric.internal.bytecode.MethodRef in project robolectric by robolectric.
the class AndroidConfigurer method configure.
public void configure(InstrumentationConfiguration.Builder builder, Interceptors interceptors) {
for (MethodRef methodRef : interceptors.getAllMethodRefs()) {
builder.addInterceptedMethod(methodRef);
}
builder.doNotAcquireClass("org.robolectric.TestLifecycle").doNotAcquireClass("org.robolectric.manifest.AndroidManifest").doNotAcquireClass("org.robolectric.RobolectricTestRunner").doNotAcquireClass("org.robolectric.RobolectricTestRunner.HelperTestRunner").doNotAcquireClass("org.robolectric.shadow.api.ShadowPicker").doNotAcquireClass("org.robolectric.res.ResourcePath").doNotAcquireClass("org.robolectric.res.ResourceTable").doNotAcquireClass("org.robolectric.ApkLoader").doNotAcquireClass("org.robolectric.res.builder.XmlBlock");
builder.doNotAcquirePackage("javax.").doNotAcquirePackage("jdk.internal.").doNotAcquirePackage("org.junit").doNotAcquirePackage("org.hamcrest").doNotAcquirePackage("org.robolectric.annotation.").doNotAcquirePackage("org.robolectric.internal.").doNotAcquirePackage("org.robolectric.pluginapi.").doNotAcquirePackage("org.robolectric.manifest.").doNotAcquirePackage("org.robolectric.res.").doNotAcquirePackage("org.robolectric.util.").doNotAcquirePackage("org.robolectric.RobolectricTestRunner$").doNotAcquirePackage("sun.").doNotAcquirePackage("com.sun.").doNotAcquirePackage("org.w3c.").doNotAcquirePackage("org.xml.").doNotAcquirePackage(// allows for android projects with mixed scala\java tests to be
"org.specs2").doNotAcquirePackage(// run with Maven Surefire (see the RoboSpecs project on github)
"scala.").doNotAcquirePackage("kotlin.").doNotAcquirePackage("io.mockk.proxy.").doNotAcquirePackage("org.bouncycastle.").doNotAcquirePackage("org.conscrypt.").doNotAcquirePackage("com.almworks.sqlite4java").doNotAcquirePackage("org.jacoco.");
builder.addClassNameTranslation("java.net.ExtendedResponseCache", "org.robolectric.fakes.RoboExtendedResponseCache").addClassNameTranslation("java.net.ResponseSource", "org.robolectric.fakes.RoboResponseSource").addClassNameTranslation("java.nio.charset.Charsets", StandardCharsets.class.getName()).addClassNameTranslation("java.lang.UnsafeByteSequence", Object.class.getName()).addClassNameTranslation("java.util.jar.StrictJarFile", Object.class.getName());
if (Util.getJavaVersion() >= 9) {
builder.addClassNameTranslation("sun.misc.Cleaner", "java.lang.ref.Cleaner$Cleanable");
}
// Instrumenting these classes causes a weird failure.
builder.doNotInstrumentClass("android.R").doNotInstrumentClass("android.R$styleable");
builder.addInstrumentedPackage("dalvik.").addInstrumentedPackage("libcore.").addInstrumentedPackage("android.").addInstrumentedPackage("com.android.internal.").addInstrumentedPackage("org.apache.http.").addInstrumentedPackage("org.ccil.cowan.tagsoup").addInstrumentedPackage("org.kxml2.");
builder.doNotInstrumentPackage("android.arch");
builder.doNotInstrumentPackage("android.support.test");
// Mockito's MockMethodDispatcher must only exist in the Bootstrap class loader.
builder.doNotAcquireClass("org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher");
for (String packagePrefix : shadowProviders.getInstrumentedPackages()) {
builder.addInstrumentedPackage(packagePrefix);
}
}
use of org.robolectric.internal.bytecode.MethodRef in project robolectric by robolectric.
the class SandboxClassLoaderTest method shouldRemapClassesWhileInterceptingMethods.
@Test
public void shouldRemapClassesWhileInterceptingMethods() throws Exception {
InstrumentationConfiguration config = configureBuilder().addClassNameTranslation(AClassToForget.class.getName(), AClassToRemember.class.getName()).addInterceptedMethod(new MethodRef(AClassThatCallsAMethodReturningAForgettableClass.class, "getAForgettableClass")).build();
setClassLoader(new SandboxClassLoader(config));
Class<?> theClass = loadClass(AClassThatCallsAMethodReturningAForgettableClass.class);
theClass.getMethod("callSomeMethod").invoke(shadow.directlyOn(theClass.newInstance(), (Class<Object>) theClass));
}
Aggregations