Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Robolectric 2.4 in Android Studio 1.1.0 & gradle 2.2.1

I am unable to configure my Android Studio Project with Robolectric. Could any one guide me step by step to build an android studio 1.1.0 project with Robolectric 2.4 & gradle 2.2.1

This sample projects work fine https://github.com/robolectric/deckard-gradle. But I am not able to implement it in my existing Android Studio projects.

//below are the detailed description


//Contents Of of project build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

//Contents Of app/src/build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "robo.testing.android.testrobo08"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets { main { java.srcDirs = ['src/main/java'] } }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'

    // Test Compile
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:2.4'
    testCompile "org.mockito:mockito-core:1.9.5"
}

//Contents Of MainActivityTest

package robo.testing.android.testrobo08.test;

import android.app.Activity;
import android.widget.TextView;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import robo.testing.android.testrobo08.MainActivity;
import robo.testing.android.testrobo08.R;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

/**
 * Created by cflap584 on 13/3/15.
 */

@RunWith(RobolectricTestRunner.class)
@Config(manifest = "./src/main/AndroidManifest.xml", emulateSdk = 18)

public class MainActivityTest {
    Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();

    TextView textView = (TextView) activity.findViewById(R.id.myTextView);

    @Test
    public void testTextView_lableName() throws Exception {
        String actual = textView.getText().toString();
        assertThat("Hello World!", equalTo(actual));
    }


}

//ERROR

robo.testing.android.testrobo08.test.MainActivityTest > testTextView_lableName STANDARD_ERROR
    WARNING: no system properties value for ro.build.date.utc

robo.testing.android.testrobo08.test.MainActivityTest > testTextView_lableName STANDARD_OUT
    DEBUG: Loading resources for robo.testing.android.testrobo08 from ././src/main/res...
    DEBUG: Loading resources for android from jar:/home/cflap584/.m2/repository/org/robolectric/android-all/4.3_r2-robolectric-0/android-all-4.3_r2-robolectric-0.jar!/res...
Gradle Test Executor 1 finished executing tests.

robo.testing.android.testrobo08.test.MainActivityTest > testTextView_lableName FAILED
    java.lang.RuntimeException: Could not find any resource  from reference ResName{robo.testing.android.testrobo08:style/Theme_AppCompat_Light_DarkActionBar} from style StyleData{name='AppTheme', parent='Theme_AppCompat_Light_DarkActionBar'} with theme null
        at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getParent(ShadowAssetManager.java:456)
        at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getAttrValue(ShadowAssetManager.java:394)
        at org.robolectric.shadows.ShadowResources.getOverlayedThemeValue(ShadowResources.java:294)
        at org.robolectric.shadows.ShadowResources.findAttributeValue(ShadowResources.java:283)
        at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:186)
        at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:44)
        at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:491)
        at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:486)
        at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:481)
        at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
        at android.content.Context.obtainStyledAttributes(Context.java:380)
        at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:147)
        at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
        at robo.testing.android.testrobo08.MainActivity.onCreate(MainActivity.java:13)
        at android.app.Activity.performCreate(Activity.java:5133)
        at org.robolectric.internal.ReflectionHelpers$3.run(ReflectionHelpers.java:64)
        at org.robolectric.internal.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:114)
        at org.robolectric.internal.ReflectionHelpers.callInstanceMethodReflectively(ReflectionHelpers.java:59)
        at org.robolectric.util.ActivityController$1.run(ActivityController.java:115)
        at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:268)
        at org.robolectric.util.ActivityController.create(ActivityController.java:111)
        at org.robolectric.util.ActivityController.create(ActivityController.java:122)
        at robo.testing.android.testrobo08.test.MainActivityTest.<init>(MainActivityTest.java:26)
like image 241
avinash kumar Avatar asked Mar 13 '15 07:03

avinash kumar


People also ask

Is Robolectric deprecated?

Robolectric is intended to be fully compatible with Android's official testing libraries since version 4.0. As such we encourage you to try these new APIs and provide feedback. At some point the Robolectric equivalents will be deprecated and removed.


1 Answers

I found the problem. In my case it was not able to find the AndroidManifest.xml file & i haven't use customRobolectricTestRunner.

Project Structure

  Gradle Version 2.2.1
  Android Plugin Version 1.1.0

project build.gradle file's content

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

App build.gradle file's content

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "robo.testing.android.testrobo"
        minSdkVersion 16
        targetSdkVersion 18
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets { main { java.srcDirs = ['src/main/java'] } }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'


// Test Compile
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:2.4'
    testCompile "org.mockito:mockito-core:1.9.5"
}

Build Variants

Test Artificat : Unit Tests

I found some reference for my solution https://github.com/robolectric/robolectric/issues/1592
@erd If you are using Gradle / Android Studio, you need to create a custom test runner that will tell Robolectric where to find your merged assets, resources, and manifest. Please see the discussion here:

http://nenick-android.blogspot.in/2015/03/android-studio-110-and-robolectric-30.html

content of src/test/java/packageName.test/CustomRobolectricRunner.java

 public CustomRobolectricRunner(Class<?> testClass)
            throws InitializationError {
        super(testClass);
        String buildVariant = (BuildConfig.FLAVOR.isEmpty()
                ? "" : BuildConfig.FLAVOR+ "/") + BuildConfig.BUILD_TYPE;
        String intermediatesPath = BuildConfig.class.getResource("")
                .toString().replace("file:", "");
        intermediatesPath = intermediatesPath
                .substring(0, intermediatesPath.indexOf("/classes"));

        System.setProperty("android.package", 
                BuildConfig.APPLICATION_ID);
        System.setProperty("android.manifest",
                intermediatesPath + "/manifests/full/" 
                        + buildVariant + "/AndroidManifest.xml");
        System.setProperty("android.resources", 
                intermediatesPath + "/res/" + buildVariant);
        System.setProperty("android.assets", 
                intermediatesPath + "/assets/" + buildVariant);
    }

Content of MainActivityTest.java

package robo.testing.android.testrobo.test;

import android.app.Activity;
import android.widget.TextView;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;

import robo.testing.android.testrobo.MainActivity;
import robo.testing.android.testrobo.R;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

/**
 * Created by cflap584 on 16/3/15.
 */

@RunWith(CustomRobolectricTestRunner.class)
public class MainActivityTest {
    Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();
    TextView textView = (TextView) activity.findViewById(R.id.myTextView);

    @Test
    public void testTextView_labelName() throws Exception {
        String actual = textView.getText().toString();
        assertThat("Hello world!", equalTo(actual));

    }

}

enter image description here

like image 168
avinash kumar Avatar answered Oct 01 '22 13:10

avinash kumar