Use PowerMockRule and RoomDatabase together RoomDatabase build error

129 views
Skip to first unread message

weijian ni

unread,
Dec 8, 2021, 12:54:51 AM12/8/21
to PowerMock
I want use powermock and RoomDatabase together for test Repository .
so like:
```
    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Before
    public void initDb() throws Exception {
        AppDatabase mDatabase = Room.inMemoryDatabaseBuilder(getContext(),
                AppDatabase.class)
                .allowMainThreadQueries()
                .build();
    }
```

but has below error:

```
class db.AppDatabase_Impl cannot be cast to class db.AppDatabase (db.AppDatabase_Impl is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @3cd4c8ee;db.AppDatabase is in unnamed module of loader org.powermock.core.classloader.javassist.JavassistMockClassLoader @2882d5a4)
java.lang.ClassCastException: class db.AppDatabase_Impl cannot be cast to class db.AppDatabase (db.AppDatabase_Impl is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @3cd4c8ee; db.AppDatabase is in unnamed module of loader org.powermock.core.classloader.javassist.JavassistMockClassLoader @2882d5a4)
        at BaseRepTest.initDb(BaseRepTest.java:93)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
        at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
        at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:83)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1864)
        at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:825)
        at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:690)
        at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
        at org.powermock.classloading.AbstractClassloaderExecutor.getResult(AbstractClassloaderExecutor.java:76)
        at org.powermock.classloading.AbstractClassloaderExecutor.invokeWithClassLoader(AbstractClassloaderExecutor.java:64)
        at org.powermock.classloading.AbstractClassloaderExecutor.executeWithClassLoader(AbstractClassloaderExecutor.java:56)
        at org.powermock.classloading.SingleClassloaderExecutor.execute(SingleClassloaderExecutor.java:33)
        at org.powermock.classloading.AbstractClassloaderExecutor.execute(AbstractClassloaderExecutor.java:40)
        at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:75)
        at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
        at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
        at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:570)
        at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:278)
        at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)

```
If only use PowerMockRule  or RoomDatabase run is ok, but  together is error. What's wrong with it and How to solve it ?


Reply all
Reply to author
Forward
0 new messages