我在Android Studio中,尝试创建单元测试。我按照Vogella tutorial的说明操作,一切正常,除非我导入并引用我要测试的模块类。例如,像这样的测试有效:
assertTrue(true);
但不是这样:
assertTrue(myMethod());
这是我要测试的模块的build.gradle:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
}
dependencies {
provided fileTree(dir: 'libs', include: '*.jar')
compile project(':Mylib')
compile project(':Mylib2')
compile project(':Mylib3')
//compile 'com.android.support:multidex:1.0.0'
compile 'org.apache.commons:commons-lang3:3.4'
// unit testing
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
}
configurations { all*.exclude group: 'com.android.support', module: 'support-v4' }
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
compileOptions.encoding = 'windows-1251'
defaultConfig {
minSdkVersion 14
targetSdkVersion 18
versionCode 1
versionName "1.0"
// Enabling multidex support.
multiDexEnabled true
}
dexOptions {
javaMaxHeapSize "4g"
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('test')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
productFlavors {
}
}
这是类" src / test / pathname / Test.java" (我按照教程的说明创建了文件夹):
package com.my.Example;
import org.junit.Test;
import com.my.Example.foo;
import static org.junit.Assert.*;
public class Test {
@Test
public void myTest() {
assertTrue("test", true); // this is ok
assertTrue(myMethod()); // this not works :(
}
}
其中myMethod()在Example类中,并返回一个布尔值。
如何解决?