Samstag, 14. März 2015

Android Studio 1.1.0 and Robolectric 3.0

Step by step guide how to use Robolectric within Android Studio


Ready to use examples can be found at https://github.com/nenick/AndroidStudioAndRobolectric

Android Project


Create or open an existing android project with Android Studio. I used the "Blank Activity with Fragment" to proof my step by step guide and choosed Android SDK 21 as my target.

Latest tested build tools version:
classpath 'com.android.tools.build:gradle:1.1.3'

Speed up Unit Test compilation (Optional)


This part is optional but improve a bit the test compiling time. Replace the "Make" task with  "Gradle-Aware-Task" for your run configuration (Run configuration -> Defaults -> JUnit -> Run External). 


1. Start with a simple JUnit Test


Next Step is to enable the new experimental Unit Test support for Android Studio. Here is a guide from google: http://tools.android.com/tech-docs/unit-testing-support

Add JUnit test dependencies


  testCompile 'junit:junit:4.12'
  testCompile "org.mockito:mockito-core:1.9.5"

And activate the experimental unit test feature: Settings / Gradle / Experimental

Now write a simple test and check if the unit test support works.

import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class SimpleUnitTest {
    @Test
    public void checkJUnitWork() {
        // failing test gives much better feedback 
        // to show that all works correctly ;)
        assertThat(true, is(false)); 
    }
}

Just right click on class or method and choose > Run and you will see a failing test, when you replace the expected value with true then the test should be successful.


2. Add initial Robolectric support


Add Robolectric as test dependency and sync your gradle configuration.

repositories {
    maven { url = "https://oss.sonatype.org/content/repositories/snapshots" }
}
dependencies {
    testCompile "org.robolectric:robolectric:3.0-SNAPSHOT"
}

Now you can use Robolectric to write tests. First we will only check if a Robolectric faked android context exists.

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.RobolectricTestRunner;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(RobolectricTestRunner.class)
public class RobolectricTest {
    @Test
    public void testIt() {
        // failing test gives much better feedback
        // to show that all works correctly ;)
        assertThat(RuntimeEnvironment.application, nullValue());
    }
}

Just right click on class or method and choose > Run and you will see a failing test, when you replace the expected value with notNullValue() then the test should be successful.

3. Setup Robolectric Tests


If you now try to use a simple Robolectric example test you will only see failing tests and strange errors which give less feedback for the root cause https://github.com/robolectric/robolectric but here are the solutions.

Add TextView with id for testing

A fresh created android project contains most times a text view with "Hello World". Give this TextView an id to be accessible from code.

Test the TextView content

Let's start with basic test based on Robolectric which will first not work but then we fix all the errors step by step.

import android.app.Activity;
import android.widget.TextView;
import com.example.myapplication.MainActivity;
import com.example.myapplication.R;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(CustomRobolectricRunner.class)
public class RobolectricTest {
    @Test
    public void testIt() {
        Activity activity = 
                Robolectric.setupActivity(MainActivity.class);

        TextView results = 
                (TextView) activity.findViewById(R.id.textView);
        String resultsText = results.getText().toString();
        
        // failing test gives much better feedback
        // to show that all works correctly ;)
        assertThat(resultsText, equalTo("Testing Android Rocks!"));
    }
}

The CustomRobolectricRunner is optional when you prefer the @Config annotation. I prefer the custom runner because then there is less configuration at Android Studio necessary.

import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

public class CustomRobolectricRunner extends RobolectricTestRunner {
    public CustomRobolectricRunner(Class<?> testClass) 
                throws InitializationError {
        super(testClass);
    }
}



Now start the test (which will fail) to get a feeling what we do at the following step.

4. Manifest location must be known


This is an important warning wich must be avoided:

WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only.

 the next try could be to adjust the "Working Directory" of the default unit test configuration pointing to the module directory "app". This will work but would us force to make custom settings to the run configuration and will not work great with multimodule projects.

With this guide, we will try to have minimal configuration effort and easy support for many setups. So here comes another solution where we make the AndroidManifest.XML location dynamic.

Remove the @Config Annotation and extend CustomRobolectricRunner constructor.

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);
}

Expected result: You will not see the warning with Android Studio or command-line and a new message should appear likely following:

DEBUG: Loading resources for com.example.myapplication from ./app/src/main/res...
DEBUG: Loading resources for android from jar:/Users/UserName/.m2/repository/org/robolectric/android-all/4.3_r2-robolectric-0/android-all-4.3_r2-robolectric-0.jar!/res...

You are ready. More examples can be found at https://github.com/nenick/AndroidStudioAndRobolectric

27 Kommentare:

  1. Dieser Kommentar wurde vom Autor entfernt.

    AntwortenLöschen
  2. Do you know, how to enable Android Studio to see both 'androidTest' and 'test' as 'tests'? Because right now AS recognizes androidTest as tests, but doesn't with 'test'.

    AntwortenLöschen
    Antworten
    1. Both visible does not work current. There was a plugin https://github.com/evant/android-studio-unit-test-plugin before native unit test support was implemented which had archive this, but don't work anymore with latest android studio/gradle tools. But I hope they will work on this https://code.google.com/p/android/issues/detail?id=149186

      Löschen
    2. Also see the answer from Eduardo

      Löschen
  3. You need to change manually the Test Artifact in the Build Variants. When you select 'Unit Test' the 'test' folder will be active (green) , when you select the 'Android Instrumentation Tests' the 'androidTest' folder will be active (green).

    AntwortenLöschen
  4. Thank you guys, it really works, though solution isn't really obvious :)

    AntwortenLöschen
  5. I get "android.content.res.Resources$NotFoundException: no such label com.example.greenflavor.debug:string/app_name"

    do you know a fix for this with the 3.0 snapshot and 1.1.3 gradle plugin?

    AntwortenLöschen
    Antworten
    1. No, not without knowing more details about your project setup. Do you see WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only? Also compare your setup with https://github.com/nenick/AndroidStudioAndRobolectric

      Löschen
  6. Uttar Pradesh Education Board will conduct counselling process of UP BTC 2020. We will update every important link of UP BTC counselling 2020 on our page. Candidates will be able to check their UP BTC 2020 Counselling result directly on our website.

    AntwortenLöschen
  7. Hey Everyone the group 4 hall ticket download is now available for all the candidates on our website.
    Click on the download link after visiting the website and download your hall ticket in easy steps.
    Thanks.

    AntwortenLöschen
  8. Really amazing content, thanks for sharing with us and keep updating! This website article is really excellent and unique. I will visit your site again. You can see the Bangladesh Education, Events, JSC, PSC, SSC, HSC, Honours, nu, Result, routine and Job circular Pureinfobd

    AntwortenLöschen
  9. Since you live in the US, you can contact our QuickBooks Helpline Number 1-833-325-0220. Our learned & highly skilled Qb experts available 24/7 to give assistance.

    AntwortenLöschen
  10. Nice post!

    Worried About QuickBooks Error ?Get in touch with QuickBooks expert for instant solution.
    Click Here to know how to fix QuickBooks Error 136

    Dial on QuickBooks Toll-free Number +1-855-977-7463.

    AntwortenLöschen
  11. Nice & Informative Blog !
    A prestigious accounting software, QuickBooks, has been designed to make the -flow of accounting painless and hassle-free. However, this software is also prone to technical issues like QuickBooks Error 30159 dial 1-855-977-7463.

    AntwortenLöschen
  12. its Amazing Blog Thanks for sharing . if you have nedd instant help then dial at
    quickbooks phone number

    AntwortenLöschen
  13. QuickBooks is most demandable software in the market for managing bussiness accounting task if you are looking quickbooks solution then contact at
    QuickBooks Customer Service for technical support

    AntwortenLöschen
  14. Dear Readers,
    In the very first article of JAIIB or DB&F, we discussed about the exams, what are they, who conducts them and why it is important.
    Now, in this article, we will be discussing about the Exam Pattern, Eligibility and Schedule of the JAIIB Examination.
    Both JAIIB and DB&F are conducted two times in a year – One in around month of May and second time in around month of November on the three consecutive Sundays of the month.
    IIBF JAIIB Exam Study Material All About JAIIB: Exam Pattern, Eligibility and Schedule

    AntwortenLöschen
  15. Very good and quick service. I would like to thank the QuickBooks Support Phone Number (855)885-5111 team for their effective assistance. Our team at QuickBooks will provide you with the best technical solutions for QuickBooks for MAC Support problems.

    AntwortenLöschen
  16. Good content!!
    We are providing best Quickbook support teamyou can contact us at.+18555484814

    AntwortenLöschen
  17. Pet Products - Buy dog food, accessories, toys, raincoat, cat food, dog cage, dog supplements, treats, etc. at the best price on All4pets. 
    best buy Pets food ONLINE IN INDIA

    AntwortenLöschen
  18. buy Best peanut butter low price
    Buy Peanut Butter Online - Musclife presents crunchy, creamy and chocolate peanut butter with best price in India, along with the assurance of authenticity.

    AntwortenLöschen
  19. We are a group of freelancer makeup artists in Chandigarh We provide make-up services for films, events, weddings, and other occasions

    AntwortenLöschen
  20. We at all4pets have varieties of pet perfume for your Dog And Cat.
    buy Dog perfume ONLINE IN INDIA

    AntwortenLöschen
  21. best whey protein powder in india Best Creatine for muscle growth - If increasing muscle mass is your main goal, this creatine supplement from Musclife is a great option.
    buy best whey protein powder in india ONLINE IN INDIA

    AntwortenLöschen
  22. Ashwagandha organic india Buy Herbal Dietary Supplements - We offer a wide range for the heart, piles, digestion, stress, joint health, memory, immunity, beauty supplements for hair, and skin
    buy Ashwagandha organic india ONLINE IN INDIA

    AntwortenLöschen
  23. Chopping board is a kitchen material. It is usually used in chopping raw meat, dairy products, fruits, vegetables, nuts. The chopping board is depend on your need.
    buy Plastic And Steel Chopping Boards ONLINE IN INDIA

    AntwortenLöschen