Java – Robolectric – AndroidStudio – Method with parameter not found testCompile()

Robolectric – AndroidStudio – Method with parameter not found testCompile()… here is a solution to the problem.

Robolectric – AndroidStudio – Method with parameter not found testCompile()

I’m trying to run some basic unit tests in AndroidStudio using Robolectric and AssertJ. I’ve used the testCompile method to add Robolectric, AssertJ, and JUnit to my build.gradle file. However, when I try to actually run the unit tests, I keep getting an error message “Method not found by testCompile()”.

I’ve tried replacing the testCompile method with androidTestCompile, as mentioned earlier here , But I couldn’t find the Robolectric, AssertJ, and JUnit classes to import into my class.


also created a very basic sample application to see if I could get Robolectric to work. Everything works fine with the app, which confuses me, find the project here

Thank you very much for some help, thank you.

Here is the log output:

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.
Total time: 2.163 secs
Could not find method testCompile() for arguments [org.robolectric:robolectric:3.0-rc2] on project ':a-fp-core'.

Here is my test class :

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;

@Config(constants = BuildConfig.class)
public class SampleTest {

    public void sampleTest() throws Exception {

String testString = "hey";



This is the a-fp-core build.gradle file:

buildscript {
   repositories {
   dependencies {
       classpath ''
       classpath "org._10ne.gradle:rest-gradle-plugin:0.3.2"

apply plugin: ''
apply plugin: ""

repositories {
   maven { url '' }
   maven { url '' }

dependencies {
    compile fileTree(include: '*.jar', dir: 'libs')
    compile project(':a-fa-core-data')
    compile ''
    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'

Folder structure:

Folder Structure

My test in Android Studio:

Unit Test


Source Set:

I can tell you about the Eclipse-based project that is based on your sourceSets:

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']

I recommend deleting your sourceSets and using the Android Studio/Intellij project structure: src/main, src/test.

Run unit tests from IDE:

Make sure that Unit Tests is turned on.

Unit testing in Android Studio

This allows you to run unit tests in the IDE. Follow the instructions in the official Android documentation:


Run unit tests from the command line:

Gradlew test

Related Problems and Solutions