I am getting ClassCastException while obtaining the shadow object here. If I remove the PowerMockRule it works but I need that for other parts of the tests. Not able to figure out the reason for the exception. (AGP 7.2.2 / Gradle 7.3.3). Any idea?
package com.example.hellopowermock;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.gms.common.ShadowGoogleApiAvailability;
@RunWith(RobolectricTestRunner.class)
public class MyTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@Test
@Config(manifest = Config.NONE, shadows = {ShadowGoogleApiAvailability.class})
public void testMethod() {
// ...
final ShadowGoogleApiAvailability shadowGoogleApiAvailability
= Shadow.extract(GoogleApiAvailability.getInstance()); // <-- Exception
final int expectedCode = ConnectionResult.SUCCESS;
shadowGoogleApiAvailability.setIsGooglePlayServicesAvailable(expectedCode);
// ...
}
}
Exception:
class com.google.android.gms.common.GoogleApiAvailability cannot be cast to class org.robolectric.internal.bytecode.ShadowedObject (com.google.android.gms.common.GoogleApiAvailability and org.robolectric.internal.bytecode.ShadowedObject are in unnamed module of loader org.powermock.core.classloader.javassist.JavassistMockClassLoader @75fc8b99) java.lang.ClassCastException: class com.google.android.gms.common.GoogleApiAvailability cannot be cast to class org.robolectric.internal.bytecode.ShadowedObject (com.google.android.gms.common.GoogleApiAvailability and org.robolectric.internal.bytecode.ShadowedObject are in unnamed module of loader org.powermock.core.classloader.javassist.JavassistMockClassLoader @75fc8b99) at org.robolectric.internal.bytecode.ShadowImpl.extract(ShadowImpl.java:14) at org.robolectric.shadow.api.Shadow.extract(Shadow.java:25) at com.example.hellopowermock.MyTest.testMethod(MyTest.java:25)
....
Gradle file:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.hellopowermock"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '11'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
testImplementation "org.robolectric:robolectric:4.8.2"
testImplementation('org.powermock:powermock-core:2.0.7')
testImplementation('org.powermock:powermock-module-junit4:2.0.7')
testImplementation('org.powermock:powermock-module-junit4-rule:2.0.7')
testImplementation('org.powermock:powermock-classloading-xstream:2.0.7')
testImplementation('org.powermock.tests:powermock-tests-utils:1.6.5')
testImplementation "org.robolectric:shadows-playservices:4.8.2"
testImplementation 'com.google.android.gms:play-services-ads:21.2.0'
testImplementation "com.google.android.gms:play-services-auth:20.3.0"
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}