Java Robolectric-AndroidStudio-找不到参数的方法testCompile()

Java Robolectric-AndroidStudio-找不到参数的方法testCompile(),java,android,unit-testing,junit,robolectric,Java,Android,Unit Testing,Junit,Robolectric,我试图在AndroidStudio中使用Robolectric和AssertJ运行一些基本单元测试。我使用testCompile方法将Robolectric、AssertJ和JUnit添加到我的build.gradle文件中。然而,当我尝试实际运行单元测试时,我继续得到一个错误,说“找不到testCompile()方法” 我已经尝试用前面提到的androidTestCompile替换testCompile方法,但是在我的SampleTest.java类中找不到可导入的Robolectric、As

我试图在AndroidStudio中使用Robolectric和AssertJ运行一些基本单元测试。我使用
testCompile
方法将Robolectric、AssertJ和JUnit添加到我的
build.gradle
文件中。然而,当我尝试实际运行单元测试时,我继续得到一个错误,说“找不到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中的测试:

源集: 我可以告诉您来自一个基于您的
源集的基于Eclipse的项目:

sourceSets {
    main {
        manifest.srcFile 'src/main/AndroidManifest.xml'
        java.srcDirs = ['src/main/java']
        resources.srcDirs = ['src/main/java']
        aidl.srcDirs = ['src/main/java']
        renderscript.srcDirs = ['src/main/java']
        res.srcDirs = ['src/main/res']
        assets.srcDirs = ['src/main/assets']
    }
}
我建议删除您的
sourceset
,并使用androidstudio/Intellij项目结构:
src/main、src/test

IDE
运行
Unit
测试: 确保已打开“单元测试”

这允许您在IDE中运行单元测试。请遵循Android官方文档中的说明:

资料来源:

从命令行运行单元测试:
gradlew测试

我不认为测试适用于Android库+
Robolectric
。你能给我们看看
a-fp-core
build.gradl
吗?真的吗?那么我们应该用类似的东西来代替吗?抱歉@JaredBurrows,我不清楚。我已经为
a-fp-core
加入了
build.gradle
,并编辑了我的帖子以正确标记它。好吧,他们最终应该一起工作,尽管让robolectric在android中工作每次都是一件非常头痛的事……好吧,我感到困惑。您的错误是“a-fp-core”。我没有看到你的“测试”文件夹。由于您刚刚从
Eclipse
AS
,因此必须重写
源集。请查看或简单地使您的文件夹像一个普通的Android Studio项目。看,很好!删除
源集
。你是从IDE运行吗?确保“beta”单元测试已打开。您将看到IDE识别“testCompile”文件夹结构。