Need insurance 18 in new york cheap?

i am an 18 year old college student in new york my parents are close to retarded, my brother is crazy and delusional and since i have a girlfriend he opposes me getting a car (jealousy) while all my…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Unit testing for Android UI by Robolectric

We can easily set up Robolectric in an android project with the help of adding the Robolectric Gradle dependency in the build.gradle file of the app project inside Android Studio.
We need to add the line below in the Gradle dependencies in order to getting started

While we’re at setting up Robolectric in our Android project test suite, we can also include the resources in our Unit tests so that our Robolectric unit tests will be able to use the Resources we add in our project, for doing this we can add the below code in our build.gradle

Our application to test is a chat application and the first thing we would like to test via Robolectric is whether we are able to initialize our ChatActivity class or not. Before actually writing our test we need to know how to actually annotate a class so that our Tests will use the Robolectric library.

In the code above we can see that we have annotated our test class with @RunWith and we’re using RobolectricTestRunner.class for running our tests, this enables our tests to use the power of Robolectric library and use the implementations of UI elements provided by the Robolectric library. We’re also using @Config annotation which helps our Robolectric to know the required details about our application like SDK version, package name, etc. We can also use different tags for SDK version and manifest files which help us in defining separate manifest file and different SDK version for a test.

Now we know how to write a Robolectric test class we can move on to initializing and checking our ChatActivity for null in a unit test. The code below describes the test which can be used to check for initialization of our activity

In the code above we can see how we initialized our ChatActivity with the help of Robolectric and then checked the activity for being null, but it’s not really a good way to initialize an Activity in a Test function, We should only initialize the Activity once in a Test class unless there’s a very good reason not to do so. To fix this issue we can use the @Before annotation on a setup function that can fix this problem for us.

By using the above code we can set the activity in a global variable and then use that activity variable in multiple tests in our test class. This way we’ll only need to initialize the Activity once and this will not harm the performance of our test suite.

Now to test the things inside of activity lets take an example of a hello world application, Suppose someone clicks a button after entering their name in a textBox and after that “Hello [Name]” shows up on the screen in a TextView in our Activity, These kind of things are really easy to test with Robolectric, as we already have an instance of our activity with us now we can all functions like findViewById and then we can also perform a click on any button and check the text in any TextView in our activity very easily in our test.

As you can see in the above code, with Robolectric it’s really easy to unit test our UI in Android and as the feedback cycle of Robolectric tests are really fast, they’re good for developing code rapidly on android. Fragments can also be tested with Robolectric including the functionality in almost the same way that we test the activities. There are helper methods to test fragments in Robolectric like “startFragment(fragment)”.

Robolectric is a really powerful tool for testing Android UI, it is still not a replacement for espresso or the UI testing frameworks that Android offers, It can be used to complement the espresso tests but espresso is better in some places where Robolectric falls like espresso isn’t affected much by newer android API’s and it runs the application on actual devices. We can test using espresso on multiple devices on device farm services to check our application compatibility with Newer and older Android devices, Which can’t be done by Robolectric. Keeping this in mind we can supplement our current Android project tests by adding Robolectric tests to provide us quick feedback for Android UI and it can help us increase our Unit test coverage to activity and fragment classes in Android.

Add a comment

Related posts:

Life is Never Going to be Just Easy

And that is because life is hard, life is scary, life is dangerous, life is unsafe but that’s just how life is. Embrace everything that life is and what it’s ever going to be, because life is so many…

A gap for God

Alexina Erickson sat at Eagle Brook Church in White Bear Lake May 10. With ears opened in the second row, she listened to one of her pastors, Steve Hupert, talk to graduating students about how to…

HOW TO CREATE A STRONG AND MEMORABLE PASSWORD

Do you realize that your digital life is almost protected with passwords? Just think about how you log into your computer. You turn it on and soon you are required to type in your password to access…