Step by step guide how to use Robolectric within Android Studio
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
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.
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
Dieser Kommentar wurde vom Autor entfernt.
AntwortenLöschenDo 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öschenBoth 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öschenAlso see the answer from Eduardo
LöschenYou 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öschenThank you guys, it really works, though solution isn't really obvious :)
AntwortenLöschenI get "android.content.res.Resources$NotFoundException: no such label com.example.greenflavor.debug:string/app_name"
AntwortenLöschendo you know a fix for this with the 3.0 snapshot and 1.1.3 gradle plugin?
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öschenUttar 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öschenHey Everyone the group 4 hall ticket download is now available for all the candidates on our website.
AntwortenLöschenClick on the download link after visiting the website and download your hall ticket in easy steps.
Thanks.
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öschenSince 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öschenNice post!
AntwortenLöschenWorried 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.
Nice & Informative Blog !
AntwortenLöschenA 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.
its Amazing Blog Thanks for sharing . if you have nedd instant help then dial at
AntwortenLöschenquickbooks phone number
QuickBooks is most demandable software in the market for managing bussiness accounting task if you are looking quickbooks solution then contact at
AntwortenLöschenQuickBooks Customer Service for technical support
Dear Readers,
AntwortenLöschenIn 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
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öschenGood content!!
AntwortenLöschenWe are providing best Quickbook support teamyou can contact us at.+18555484814
Thanks for sharing amazing blog. Here the Mistery of yoga can change your mind day to day. meditation basics yoga , yoga benefits , types of yoga , yoga history , health benefit yoga , yoga pose , yoga asanas - theyogainfo.com you reach us at
AntwortenLöschenPet Products - Buy dog food, accessories, toys, raincoat, cat food, dog cage, dog supplements, treats, etc. at the best price on All4pets.
AntwortenLöschenbest buy Pets food ONLINE IN INDIA
buy Best peanut butter low price
AntwortenLöschenBuy Peanut Butter Online - Musclife presents crunchy, creamy and chocolate peanut butter with best price in India, along with the assurance of authenticity.
We are a group of freelancer makeup artists in Chandigarh We provide make-up services for films, events, weddings, and other occasions
AntwortenLöschenWe at all4pets have varieties of pet perfume for your Dog And Cat.
AntwortenLöschenbuy Dog perfume ONLINE IN INDIA
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.
AntwortenLöschenbuy best whey protein powder in india ONLINE IN INDIA
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
AntwortenLöschenbuy Ashwagandha organic india ONLINE IN INDIA
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.
AntwortenLöschenbuy Plastic And Steel Chopping Boards ONLINE IN INDIA