Requirements:
- Your Project must use at a minimum platform 2.2 Google API level 8. (This is the most cases)
- You must use Junit 4 Library and not Junit 3.
- Download the newst jar file from here: robolectric-X.X.X-all.jar
- Create your Android project normally.
- select your project and add a new folder called test like the pic bellow.
Creating a Junit Test Project
Create a simple Java-Project (attention not another Android-Project) with the same name as your Android project but with the suffix -Test at the end and click next. (Attenion: do not press finish)
Ex: MyProject and MyProjectTest
Select the src-folder, right mouse click and select "remove source folder" from build path. (the src folder will change from source into normal folder. see picture bellow after point 4)
Select the src-folder, right mouse click and take "Link additional source" and browse the created test folder from your Android Project and then click finish. The result will be something like point 1 in the pic above.
Create a folder called lib in your test project like point 4 in the picture above and copy/paste the downloaded roboelectric-x.x.x-all.jar into it.
Now select your test project, right mouse click, select Build Path > Configure Build Path... > select tab libraries > add library > Junit > next > select Junit 4 > click finish but stay in this dialog.
Click add jars... > select your test project (in this example here: MyProjectTest) > lib > select roboelectric-x.x.x-jar > click ok but stay in this dialog. (see picture bellow point 1)
Click add external jars... > [browse the location from your android-sdk] > platforms > android 8 > select android.jar > click ok but stay in this dialog. (see picture bellow point 1)
Click add external jars... > [browse the location from your android-sdk] > add-ons> addons-google_apis-android-8 > libs > select maps.jar > click ok. (see picture bellow point 1)
Select the Tab Projects > add your test project to it like the picture bellow. After that press the refresh key (F5) and run a project clean. (Project > clean...) (see picture bellow)
Setup your test run configuration
Attention: This step is very important. It will not work, if you don't do it.
Select menu run > run configurations... > double click Junit > and follow the steps in the picture bellow:
Write your first test to validate all those steps
Go back to your test project (in this case here MyProjectTest) and create a simple class in the test folder called MyActivityTest and copy the next code lines into it.
package com.example; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; import org.junit.Test; import org.junit.runner.RunWith; import com.example.myproject.MainActivity; import com.example.myproject.R; import com.xtremelabs.robolectric.RobolectricTestRunner; @RunWith(RobolectricTestRunner.class) public class MyActivityTest { @Test public void shouldHaveHappySmiles() throws Exception { String hello = new MainActivity().getResources().getString(R.string.hello_world); assertThat(hello, equalTo("Hello world!")); } }Run the test
Menu > Run > Run Configurations... > select Junit > select your created MyProjectTestConfiguration and run it. If everything went well, we should get something like this:
So bye bye slow emulator... ;-) happy coding!
😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO 😱👇
Be sure to read, it will change your life!
Show your work by Austin Kleon: https://amzn.to/34NVmwx
This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practices: https://amzn.to/30WQSm2
Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv
This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp
Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy
😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO 😱👆