我正在尝试在Android Studio中使用Robolectric和AssertJ进行基本的单元测试。我已经在
build.gradle
文件中使用testCompile
方法添加了Robolectric、AssertJ和JUnit。然而,当我尝试运行单元测试时,我仍然会收到一个错误,提示“找不到testCompile()方法”。我曾试图按这里提到的用androidTestCompile
替换testCompile
方法,但是然后在我的SampleTest.java
类中无法找到Robolectric、AssertJ和JUnit类以导入。此外,我创建了一个非常基本的示例应用程序来查看是否能让Robolectric正常工作。一切都正常,这让我感到困惑。您可以在这里找到该项目。非常感谢您的帮助。以下是日志输出:Testing started at 4:45 PM ...
4:45:18 PM: Executing external tasks 'cleanTest test --tests com.master.SampleTest'...
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/dthacker/Code/Repos/Default/CoreV2/a-fp-core/build.gradle' line: 45
* What went wrong:
A problem occurred evaluating project ':a-fp-core'.
> Could not find method testCompile() for arguments [org.robolectric:robolectric:3.0-rc2] on project ':a-fp-core'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 2.163 secs
Could not find method testCompile() for arguments [org.robolectric:robolectric:3.0-rc2] on project ':a-fp-core'.
以下是我的测试类:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class SampleTest {
@Test
public void sampleTest() throws Exception {
String testString = "hey";
assertThat(testString).isNotNull();
}
}
以下是a-fp-core的build.gradle文件:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath "org._10ne.gradle:rest-gradle-plugin:0.3.2"
}
}
apply plugin: 'com.android.library'
apply plugin: "org.10ne.rest"
repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
maven { url 'http://lorenzo.villani.me/android-cropimage/' }
}
dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile project(':a-fa-core-data')
compile 'com.google.code.gson:gson:2.3'
compile 'com.mcxiaoke.volley:library:1.0.15'
testCompile "org.robolectric:robolectric:3.0-rc2"
testCompile "junit:junit:4.12"
testCompile "org.assertj:assertj-core:1.7.0"
}
android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
文件夹结构:
}
我在Android Studio中进行的测试:
a-fp-core
的build.gradle
并编辑了我的帖子以正确标记它。 - Andrea ThackerEclipse
转到AS
,所以你需要覆盖你的sourceSets
。请参考或者简单地将你的文件夹设置成一个正常的Android Studio项目。请参考https://dev59.com/1WEh5IYBdhLWcg3wXCVL#22511405。 - Jared Burrowsbuild.gradle
文件吗? - Jared BurrowssourceSets
。你是从 IDE 运行吗?确保你已经开启了“beta”单元测试。你会看到 IDE 识别出了“testCompile”文件夹结构。 - Jared Burrows