Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android jUnit Test java.lang.NoClassDefFoundError: android/database/sqlite/SQLiteOpenHelper

Tags:

android

junit

I am trying to run a unit tests which mock a child class of SQLiteOpenHelper but I am getting the following error.

java.lang.NoClassDefFoundError: android/database/sqlite/SQLiteOpenHelper

    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    ...
at org.dfhu.vpodplayer.util.JsonExporterTest.export(JsonExporterTest.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    ...

My Gradle file looks like

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion '25.0.1'
    defaultConfig {
        applicationId "org.dfhu.vpodplayer"
        minSdkVersion 18
        targetSdkVersion 24
        versionCode 1
        versionName "1.2"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.7
        sourceCompatibility 1.7
    }
}


buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'

    testCompile 'junit:junit:4.12'
    testCompile 'org.powermock:powermock-api-mockito:1.6.5'
    testCompile 'org.powermock:powermock-module-junit4:1.6.5'
}

This did work before I reinstalled, Android Studio and updated to builtToolsVersion '25.0.1'.

I am looking for a way to get my android dependencies in my test runner.

like image 421
Victory Avatar asked Mar 16 '17 20:03

Victory


2 Answers

Occasionally Android Studio does not correctly load Android classes into the classpath for testing. In our case it's usually android.os.Parcelable. But the CI always works fine.

So running ./gradlew test should always work in such occasions and with some luck fix the dependencies for Android Studio.

like image 136
tynn Avatar answered Oct 12 '22 16:10

tynn


(This one has been confusing and infuriating me for a while now and I'm super glad I've finally got to the bottom of it!)

The first thing to note is that after running the following command in your project...

gradle clean build

... you should see a build/generated/mockable-android-XX.jar file in your workspace. This file is generated by the mockableAndroidJar Gradle Task which is run as part of the test Gradle Task which is run as part of the build Gradle Task.

The problem occurs when this file is missing from your workspace but Gradle unfortunately estimates the mockableAndroidJar Task to be up to date and therefore skips executing it. You can verify this by running the following command...

gradle mockableAndroidJar --info

... and you'll see a message something as follows...

Skipping task ':Android:mockableAndroidJar' as it is up-to-date (took 0.001 secs).

The solution is to force run the mockableAndroidJar Gradle Task which you can do by running the following command...

gradle mockableAndroidJar --rerun-tasks

... And Voila you should now see the build/generated/mockable-android-XX.jar file reappear in your workspace and glad times your Android unit tests will be running again.

like image 23
Adil Hussain Avatar answered Oct 12 '22 15:10

Adil Hussain