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 SampleTest.java class.

I

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

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;

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

@Test
    public void sampleTest() throws Exception {

String testString = "hey";

assertThat(testString).isNotNull();
    }

}

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

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

Folder structure:

Folder Structure

My test in Android Studio:

Unit Test

Solution

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:

Source: http://tools.android.com/tech-docs/unit-testing-support

Run unit tests from the command line:

Gradlew test

Related Problems and Solutions