Skip to content Skip to sidebar Skip to footer

Robolectric Unit Test Failing With Android Studio 2.3 Updates

All my Unit Test started throwing this error: No such manifest file: build\intermediates\bundles\debug\AndroidManifest.xml java.lang.NullPointerException at org.robolectric.sh

Solution 1:

Set it by default, so every new test configuration that you create will inherit it:

default setting

Run > Edit Configurations... > Defaults > Android JUnit

in Working directory enter: $MODULE_DIR$

Solution 2:

Android studio is looking at APP folder to run tests from AS 2.3 version. If you have Custom Roboelectric Runner extending RoboelectricTestRunner, you can override the working directory inside that, instead of setting WORKING DIR from edit configurations every time.

Here is my code.

`public class CustomRobolectricRunner extends RobolectricTestRunner {

// Build output location for Android Studio 2.2 and olderprivatestaticfinalStringBUILD_OUTPUT="build/intermediates";

// Build output location for Android Studio 2.3 and newerprivatestaticfinalStringBUILD_OUTPUT_APP_LEVEL="app/build/intermediates";

publicCustomRobolectricRunner(Class<?> testClass)throws InitializationError {
    super(testClass);
}

@Overrideprotected AndroidManifest getAppManifest(Config config) {
    FileFsFileres= FileFsFile.from(BUILD_OUTPUT, "/res/merged", BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE);
    FileFsFileassets= FileFsFile.from(BUILD_OUTPUT, "assets", BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE);
    FileFsFilemanifest= FileFsFile.from(BUILD_OUTPUT, "manifests", "full", BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE, "AndroidManifest.xml");

    // If above files does not exist in the specified paths, look them at the app folder level. This is needed// as Android studio 2.3 changed the working directory.if (!manifest.exists()) {
        manifest = FileFsFile.from(BUILD_OUTPUT_APP_LEVEL, "manifests", "full", BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE, "AndroidManifest.xml");
    }

    if (!res.exists()) {
        res = FileFsFile.from(BUILD_OUTPUT_APP_LEVEL, "/res/merged", BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE);
    }

    if (!assets.exists()) {
        assets = FileFsFile.from(BUILD_OUTPUT_APP_LEVEL, "assets", BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE);
    }

    AndroidManifestandroidManifest=newAndroidManifest(manifest, res, assets) {
        @Overridepublic Class getRClass() {
            return R.class;
        }

        publicintgetTargetSdkVersion() {
            /**
             * Lollipop is currently the highest version supported in 3.0.
             */return Build.VERSION_CODES.LOLLIPOP;
        }
    };

    return androidManifest;
}`

Post a Comment for "Robolectric Unit Test Failing With Android Studio 2.3 Updates"