Android jUnit测试java.lang.NoClassDefFoundError:android/database/sqlite/SQLiteOpenHelper [英] Android jUnit Test java.lang.NoClassDefFoundError: android/database/sqlite/SQLiteOpenHelper

查看:108
本文介绍了Android jUnit测试java.lang.NoClassDefFoundError:android/database/sqlite/SQLiteOpenHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行模拟SQLiteOpenHelper的子类的单元测试,但出现以下错误.

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)
    ...

我的Gradle文件看起来像

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'
}

在我重新安装Android Studio并将其更新为BuiltToolsVersion '25 .0.1'之前,此方法确实有效.

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

我正在寻找一种在测试运行程序中获取android依赖项的方法.

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

推荐答案

有时,Android Studio无法将Android类正确地加载到类路径中进行测试.在我们的情况下,通常为android.os.Parcelable.但是CI总是可以正常工作.

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.

因此,在某些情况下运行./gradlew test应该总是可以的,并且可以通过运气修复Android Studio的依赖项.

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

这篇关于Android jUnit测试java.lang.NoClassDefFoundError:android/database/sqlite/SQLiteOpenHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆