我正在尝试使用Roboelectric来测试Android应用中的位图相关功能。下面我在运行测试时附加了源代码以及gradle和结果。
试验编号:
@RunWith(RobolectricTestRunner.class)
public class ExampleUnitTest {
static Bitmap bmp;
@BeforeClass
public static void setUp(){
bmp = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.ic_info_black_24dp);
}
}
这是build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'jacoco-android'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "com.example.fabian.tinf15b4_lsmf"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
dependencies {
compile files('libs/httpclient-4.5.1.jar')
compile fileTree(include: ['*.jar'], dir: 'libs')
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:design:24.2.1'
compile 'com.android.support:support-vector-drawable:24.2.1'
testCompile 'junit:junit:4.12'
compile files('libs/themoviedbapi-4.3.jar')
testCompile "org.robolectric:robolectric:3.4-rc1"
compile files('libs/api-common-2.0.jar')
compile files('libs/commons-codec-1.9.jar')
compile files('libs/commons-lang3-3.4.jar')
compile files('libs/httpcore-4.4.3.jar')
compile files('libs/jackson-annotations-2.7.1.jar')
compile files('libs/jackson-core-2.7.1.jar')
compile files('libs/jackson-databind-2.7.1.jar')
compile files('libs/slf4j-api-1.7.16.jar')
}
这是运行时的结果:
java.lang.NoClassDefFoundError: com.xtremelabs.robolectric.bytecode.RobolectricClassLoader
at com.xtremelabs.robolectric.RobolectricTestRunner$InstrumentDetector$1.isInstrumented(RobolectricTestRunner.java:668)
at com.xtremelabs.robolectric.RobolectricTestRunner.isInstrumented(RobolectricTestRunner.java:246)
at com.xtremelabs.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:112)
at com.xtremelabs.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:99)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:77)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runner.Computer.getRunner(Computer.java:40)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at org.junit.runners.Suite.<init>(Suite.java:81)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:789)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:753)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:354)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:260)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1873)
任何人都知道为什么会失败?