ligi
ligi

Reputation: 39519

RobolectricContext missing for gradle based project

with v2.0 of robolectric and a gradle based project I am facing the problem of a missing RobolectricContext for the runner. It works with testCompile group: 'org.robolectric', name: 'robolectric', version: '2.0-alpha-2'

and fails with testCompile group: 'org.robolectric', name: 'robolectric', version: '2.0'

I have the feeling that my problem is in my gradle build file, but I found no way to fix it yet:

apply plugin : 'java-robolectric'
apply plugin : 'idea'

// get 'java-robolectric' from Maven Central
buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    // use version 2.0 for Robolectric 2.0
    classpath group: 'com.stanfy.android', name: 'gradle-plugin-java-robolectric', version: '2.0'
  }
}
sourceSets {
    main {
        java {
            srcDir 'src/java'
        }
    }
}

version = '0.9'

javarob {
    packageName = 'org.ligi.androidhelper'
}

test {
    scanForTestClasses = false
    include "**/*Test.class"
}

repositories {
    mavenCentral()
}

test {
    afterTest { desc, result ->
        println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
    }
}

dependencies {
    compile fileTree(dir : 'libs', include : '*.jar')

    testCompile group: 'junit', name: 'junit', version: '4.10'
    testCompile group: 'org.mockito', name: 'mockito-core', version: '1.8.0'

    compile group: 'com.google.android', name: 'android', version: '4.1.1.4'
    testCompile group: 'org.robolectric', name: 'robolectric', version: '2.0'
}

that is the error I get:

ligi@ligi-tp:~/git/AndroidHelper$ gradle test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
Executing test classMethod [org.ligi.androidhelper.test.CheckBoxHelperTest] with result: FAILURE

org.ligi.androidhelper.test.CheckBoxHelperTest > classMethod FAILED
    java.lang.RuntimeException
        Caused by: java.lang.RuntimeException
Executing test classMethod [org.ligi.androidhelper.test.BitmapHelperTest] with result: FAILURE

org.ligi.androidhelper.test.BitmapHelperTest > classMethod FAILED
    java.lang.RuntimeException
        Caused by: java.lang.RuntimeException

2 tests completed, 2 failed
:test FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///home/ligi/git/AndroidHelper/build/reports/tests/index.html

* 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: 11.723 secs

The full source is here: https://github.com/ligi/AndroidHelper

Upvotes: 3

Views: 468

Answers (2)

ligi
ligi

Reputation: 39519

a workaround is not to remove the Runner.java - because it always gets autogenerated by the robolectric gradle plugin with the faulty code ( usage of RobolectricContex ) then. The trick is to modify this file - even though you actually don't intend to make use of it - mine looks like this:

import java.io.File;
import org.junit.runners.model.InitializationError;
import org.robolectric.AndroidManifest;
import org.robolectric.RobolectricTestRunner;

/**                                                    
 * Use this runner instead of RobolectricTestRunner with @RunWith annotation.
 */
public class Runner extends RobolectricTestRunner {

    public Runner(final Class<?> testClass) throws InitializationError {
        super(testClass);
    }

}

Upvotes: 0

Ben Weiss
Ben Weiss

Reputation: 17922

The class RobolectricContext is not required any more. Further it does not exist in Robolectric 2.0. You can simply override methods from within the RobolectricTestRunner.

For example, finding the AndroidManifest.xml can be achieved via:

@Override
protected AndroidManifest createAppManifest(FsFile manifestFile) {
  if (!manifestFile.exists()) {
    manifestFile = Fs.fileFromPath("pathToMy/AndroidManifest.xml");
  }
  return super.createAppManifest(manifestFile);
}

Upvotes: 2

Related Questions