Skip to content

Getting error during unit test: com.thoughtworks.xstream.converters.ConversionException: Failed to transform class with name io.realm.Realm. Reason: rx.Observable : Failed to transform class with name io.realm.Realm. Reason: rx.Observable - #3478

@Hesamedin

Description

@Hesamedin

Hi, I;m following the ExampleRealmTest in order to write my test cases. However I'm getting following error:

com.thoughtworks.xstream.converters.ConversionException: Failed to transform class with name io.realm.Realm. Reason: rx.Observable : Failed to transform class with name io.realm.Realm. Reason: rx.Observable
---- Debugging information ----
message             : Failed to transform class with name io.realm.Realm. Reason: rx.Observable
cause-exception     : java.lang.IllegalStateException
cause-message       : Failed to transform class with name io.realm.Realm. Reason: rx.Observable
class               : org.junit.internal.runners.statements.InvokeMethod
required-type       : org.junit.internal.runners.statements.InvokeMethod
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/next/val$invoker/target
line number         : 13
class[1]            : org.robolectric.RobolectricTestRunner$HelperTestRunner$1
class[2]            : org.junit.internal.runners.statements.RunBefores
class[3]            : org.powermock.modules.junit4.rule.PowerMockStatement
class[4]            : org.powermock.modules.junit4.rule.PowerMockStatement$1
version             : not available
-------------------------------

    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1206)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1190)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1061)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1052)
    at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:54)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:57)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:250)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:176)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:142)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.IllegalStateException: Failed to transform class with name io.realm.Realm. Reason: rx.Observable
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:267)
    at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:180)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:70)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
    at java.lang.Class.getDeclaredFields(Class.java:1916)
    at com.thoughtworks.xstream.converters.reflection.FieldDictionary.buildMap(FieldDictionary.java:136)
    at com.thoughtworks.xstream.converters.reflection.FieldDictionary.fieldOrNull(FieldDictionary.java:113)
    at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.getFieldOrNull(PureJavaReflectionProvider.java:193)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.canAccess(AbstractReflectionConverter.java:67)
    at com.thoughtworks.xstream.converters.reflection.ReflectionConverter.canConvert(ReflectionConverter.java:42)
    at com.thoughtworks.xstream.core.DefaultConverterLookup.lookupConverterForType(DefaultConverterLookup.java:56)
    at com.thoughtworks.xstream.XStream$1.lookupConverterForType(XStream.java:498)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:56)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    ... 57 more
Caused by: javassist.NotFoundException: rx.Observable
    at javassist.ClassPool.get(ClassPool.java:452)
    at javassist.bytecode.Descriptor.toCtClass(Descriptor.java:592)
    at javassist.bytecode.Descriptor.getReturnType(Descriptor.java:489)
    at javassist.CtBehavior.getReturnType0(CtBehavior.java:306)
    at javassist.CtMethod.getReturnType(CtMethod.java:219)
    at org.powermock.core.transformers.impl.MainMockTransformer.modifyMethod(MainMockTransformer.java:163)
    at org.powermock.core.transformers.impl.MainMockTransformer.allowMockingOfStaticAndFinalAndNativeMethods(MainMockTransformer.java:133)
    at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:65)
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:252)
    ... 75 more

In my root build.gradle file:

buildscript {
    repositories {
        jcenter()
        mavenCentral() // for NewRelic
        maven { url 'https://maven.fabric.io/public' }
        maven { url 'http://maven.apptimize.com/artifactory/repo' }
        maven { url 'https://zendesk.artifactoryonline.com/zendesk/repo' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'io.fabric.tools:gradle:1.+'
        classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.3.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath "com.newrelic.agent.android:agent-gradle-plugin:5.+"
        classpath 'com.google.gms:google-services:3.0.0'
        classpath "io.realm:realm-gradle-plugin:1.2.0"
    }
}

ext {
    androidConfiguration = {
       ....
    }

    // App dependencies
    PlayServicesLibVersion = '9.0.0'
    SupportLibVersion = '23.2.1'
    JunitVersion = '4.12'
    MockitoVersion = '1.10.19'
    PowerMockito = '1.6.4'
    RobolectricVersion = '3.1.2'
    HamcrestVersion = '1.3'
    RunnerVersion = '0.4.1'
    RulesVersion = '0.4.1'
    EspressoVersion = '2.2.2'
    DaggerVersion = '2.6'
    GooglePhoneLib = '7.5.0'
}

I have many things in my app build.gradle file but my unit tests are not here. I just added apply plugin: 'realm-android' at the top of file.

I have an SDK module the acts as library for app module. Its build.gradle file contains

apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'realm-android'

android {
...
}

dependencies {
...
    // Robolectric testing
    testCompile "junit:junit:${JunitVersion}"
    testCompile "org.mockito:mockito-core:${MockitoVersion}"
    testCompile "org.robolectric:robolectric:${RobolectricVersion}"
    testCompile "org.robolectric:shadows-support-v4:${RobolectricVersion}"
    testCompile "org.powermock:powermock-module-junit4:${PowerMockito}"
    testCompile "org.powermock:powermock-module-junit4-rule:${PowerMockito}"
    testCompile "org.powermock:powermock-api-mockito:${PowerMockito}"
    testCompile "org.powermock:powermock-classloading-xstream:${PowerMockito}"
}

And finally my test class. It is almost as same as ExampleRealmTest class. Differences are:

  1. I replaced @RunWith(RobolectricGradleTestRunner.class) by @RunWith(RobolectricTestRunner.class) as RobolectricGradleTestRunner has deprecated.
  2. You are importing RealmLog from import io.realm.log.RealmLog; while I import if from import io.realm.internal.log.RealmLog; (when I hit Alt+Enter this library finds).
import com.grabtaxi.passenger.BuildConfig;
import com.grabtaxi.passenger.db.dao.RealmPOIManager;
import com.grabtaxi.passenger.model.PointOfInterest;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.List;

import io.realm.Realm;
import io.realm.internal.log.RealmLog;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.internal.verification.VerificationModeFactory.times;
import static org.powermock.api.mockito.PowerMockito.doCallRealMethod;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 19)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@SuppressStaticInitializationFor("io.realm.internal.Util")
@PrepareForTest({Realm.class, RealmLog.class})
public class RealmPOIManagerTest
{
    @Rule
    public PowerMockRule rule = new PowerMockRule();
    private Realm mockRealm;

    @Before
    public void setUp() throws Exception
    {
        mockStatic(RealmLog.class);
        mockStatic(Realm.class);

        Realm mockRealm = PowerMockito.mock(Realm.class);
        when(Realm.getDefaultInstance()).thenReturn(mockRealm);

        this.mockRealm = mockRealm;
    }

    @Test
    public void shouldBeAbleToGetDefaultInstance() {
        assertThat(Realm.getDefaultInstance(), is(mockRealm));
    }

}

Any help would be appreciated. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions