How to Test your Android App with Android Test Framework
The typical JUnit test cannot be used on JVM as the different options in the Android program JAR file “android.jar” are only just placeholder. Calling them may result in RuntimeErrors. The reason why this happens is that the android.jar file does not feature any Android framework code, instead it only checks for the type signatures and methods etc. Basically, the JAR file is only used for the Java compiler prior to deployment of the app on any Android device. Once the application is loaded on to an Android device, it uses the android.jar file on that specific Android device.
It may become impossible for you to do the testing of Android framework classes on the JVM directly if there are no additional libraries. To do the Android class testing, you must run them on an Android device itself or use an emulator, which however consumes a lot of time. There are unit tests as well as functional tests. Unit test is basically used to test the functionality of a particular component only. On the other hand, functional testing is more comprehensive to check whether the activity has correctly started.
Android Framework Test Hooks
Instrumentation: The Android testing API put forth hooks into the component lifecycle of Android. Such hooks are collectively termed as instrumentation API, which allow the tests to manage and control the lifecycle of an Android application. Instrumentation API also lets you run the basic Android project as well as the test project simultaneously, and thereby facilitating the test project to call methods directly from the basic Android project. For instance, the getActivity() function can be called directly, which initiates an activity and returns the tested activity. Later, you can call finish() method and again the getActivity() to test whether the application is getting back to its desired state properly.
Different Test Classes & Annotations in Android
Android assertion & mock classes: The testing API of Android also provides the ViewAsserts and MoreAsserts classes in addition to the basic JUnit Assert class. The mock classes let you run stand-alone tests from an already running system by overriding the normal operations. Android is also providing several mock classes in the packages like android.test and android.test.mock etc.
Android test classes: The test framework of Android is currently based on JUnit3. As a result, the tests need to extend TestCase class and also every test methodologies should be started with a test. AndroidTestCase can be used to test the components of Android apps, which do not have any visible parts. Some of the test classes are – 1) AndroidTestCase, which provides different methods to permissions, 2) ApplicationTestCase, which offers methods to test the process and functionality of the application objects, and 3) InstrumentationTestCase, which is the base class in terms of utilizing instrumentation methods.
Android Activity Testing
In order to run a stand-alone activity testing, you can use the class as ActivityUnitTestCase. This will allow you to inspect the framework of an activity in order to check whether the action is getting triggered as desired. Here the intents may not be sent to the basic Android system, instead you can make use of the method of getStartedActivityIntent() in order to access an intent and do the validation.
ActivityUnitTestCase is generally used to check the layouts and to test the activities in isolation. ActivityInstrumentationTestCase2 is a functional testing which can be written with this specific class. The direct interaction with the Android infrastructure is executed via the getInstrumentation() method. This specific class will let you to send click events.
In order to run a service testing, you can use the ServiceTestCase class. This consists of startService() as well as bindService() methods in order to communicate with the service. It is also deemed to be wise to test whether the service correctly manages various calls from the startService(). When startService() is called first time, it triggers the onCreate() function of the specific service; However, when startService() command is repeated, it calls onStartCommand() method.
“Mark Wilston is working with PixelCrayons.com, an outsourcing and consulting firm in India. PixelCrayons providing Android Apps Development services including twitter bootstrap development. as per clients needs”