Issue
Robolectric crash when trying to rungradlew test or gradlew testDebugUnitTest
with this error
> Task :app:testDebugUnitTest
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.robolectric.util.ReflectionHelpers$7 (file:/C:/Users/a.yasser/.gradle/caches/modules-2/files-2.1/org.robolectric/shadowapi/3.8/d638f001e81ef737bb35db2964312360cc996a94/shadowapi-3.8.jar)
to method java.lang.ClassLoader.getPackage(java.lang.String)
WARNING: Please consider reporting this to the maintainers of org.robolectric.util.ReflectionHelpers$7
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
ash.gaseelydriver.ui.activities.MainActivityTest > shouldNotBeNull FAILED
java.lang.NoClassDefFoundError
Caused by: java.lang.ClassNotFoundException
Caused by: java.lang.RuntimeException
Caused by: java.lang.IllegalArgumentException
this problem only happen if I run from command line I have tried multiple solutions but none of them worked including
- delete .gradle file
- delete .idea\libraries
- delete clean/ rebuild project
- downgrading and upgrading robolectric versions
- upgrading my whole project compile version and taget sdk to 28
- apgrading the gradle to version from 3.1.2 to 3.2.0
here is the gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "ash.gaseelydriver"
minSdkVersion 17
targetSdkVersion 28
versionCode 2
versionName "1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
storeFile file('ASH_Mobile.jks')
storePassword "Ahmedsh125"
keyAlias "ash_mobile"
keyPassword "Ahmedsh125"
}
}
buildTypes {
release {
zipAlignEnabled true
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
buildToolsVersion '28.0.2'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-places:16.0.0'
implementation 'com.wang.avi:library:2.1.3'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.intuit.sdp:sdp-android:1.0.4'
implementation 'com.intuit.ssp:ssp-android:1.0.5'
implementation 'com.nineoldandroids:library:2.4.0'
implementation 'me.dm7.barcodescanner:zxing:1.9.8'
testImplementation "org.robolectric:robolectric:3.8"
testImplementation 'org.mockito:mockito-core:2.7.22'
androidTestImplementation 'org.mockito:mockito-android:2.7.22'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-core:16.0.4'
testImplementation 'com.openpojo:openpojo:0.8.10'
}
apply plugin: 'com.google.gms.google-services'
here is a sample test class
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 27)
public class MainActivityTest {
private MainActivity activity;
@Before
public void setUp() {
activity = Robolectric.buildActivity(MainActivity.class).create()
.resume()
.get();
}
@Test
public void shouldNotBeNull() throws Exception {
assertNotNull(activity);
}
Solution
When using CLIs, gradle will use the java version installed on your machine. In my case, the Java version installed on my machine was the version 10, which is not yet supported by Android. Your tests were running on Android Studio because it uses an embedded JDK, which is supported by Android. Now to solve your problem, first start by checking the version of Java installed on your machine by running the following command:
java -version
If the version is 1.10.xxx, you should:
- download and install the JDK 8 via this link.
point your environment variable JAVA_HOME to the new path of JDK 8 installed by adding this line in your bash_profile (on Mac) :
export JAVA_HOME = "$ (/ usr / libexec / java_home -v 1.8)"
re-run your tests via a new terminal window
Answered By - AhmedZah
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.