【发布时间】:2015-04-14 21:58:49
【问题描述】:
我正在尝试在 AndroidStudio 中使用 Robolectric 和 AssertJ 运行一些基本的单元测试。我使用testCompile 方法将Robolectric、AssertJ 和JUnit 添加到我的build.gradle 文件中。但是,当我尝试实际运行单元测试时,我继续收到一条错误消息“找不到方法 testCompile()”。
我已经尝试用androidTestCompile 替换testCompile 方法,如here 所述,但是在我的SampleTest.java 类中找不到可以导入的Robolectric、AssertJ 和JUnit 类。
我还创建了一个非常基本的示例应用程序,看看我是否可以让 Robolectric 工作。该应用程序一切正常,这让我感到困惑,该项目可以在 here 找到。
非常感谢一些帮助,谢谢。
这是日志输出:
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 中的测试:
【问题讨论】:
-
我认为测试不适用于 Android 库 +
Robolectric。你能告诉我们build.gradl的a-fp-core吗? -
真的吗?那么我们应该改用tools.android.com/tech-docs/unit-testing-support 之类的东西吗?抱歉@JaredBurrows,我不清楚。我已经将
build.gradle包含在a-fp-core中,并编辑了我的帖子以正确标记它。 -
好吧,some people say 他们最终应该一起工作,尽管每次让 robolectric 在 android 中工作都是一件令人头疼的事情......
-
好吧,我弄糊涂了。您的错误显示“a-fp-core”。我看不到的一件事是您的“测试”文件夹。由于您刚刚从
Eclipse来到AS,因此您必须覆盖您的sourceSets。请查看或简单地将您的文件夹设置为普通的 Android Studio 项目。见stackoverflow.com/a/22511405/950427。 -
非常好!删除
sourceSets。你是从 IDE 运行的吗?确保您打开了“beta”单元测试。您将看到 IDE 识别“testCompile”文件夹结构。
标签: java android unit-testing junit robolectric