Testing in Kotlin


Testing is the interface between developer and user and it helps to increase profits. Testing is used to provide quality applications to the customer and used to solve bugs in the application.

Unit Tests

This test aims at the class level and it tests only the low-level function in the application. A unit test checks deterministic relations between input and the output of the method call and it may not include the static variable of class instance.
Standard Unit Test
A standard unit test runs without dependency on the hardware of the device and in the Android framework classes. They are useful for libraries ad some graphical user interface functionalities. If the application classes contain method calls and it can anticipate the call result in various sets of inputs, using the standard unit tests. It is easy to add a unit test to the application. While starting a new project, the unit test is created in it. A sample test class is also created in it. Then it can add more test sections in the source code.
Stubbed Android Framework
The gradle plugin is used for executing the unit test which contains the stubbed versions of the Android framework and it throws an error when it is called. The behavior is changed in the application build.gradle file with the below code:
  1. android {  
  2.     testOptions {  
  3.         unitTests.returnDefaultValues = true  
  4.     }  
  5. }   
Simulated Android framework
To access android classes from inside unit tests using the community supported roboelectric framework as unit test implementation. The roboelectric is used to simulate clicking buttons, reading, and writing text and graphical user interface related activities. The below code is used to use roboelectric in the application build.gradle.
  1. android {  
  2.     testOptions {  
  3.         unitTests {  
  4.             includeAndroidResources = true  
  5.         }  
  6.     }  
  7. }  
  8. dependencies {  
  9.     testImplementation "org.roboelectric : roboelectric : 3.8"  
  10. }   
A test class that simulates the click on button and then check the click action in TextView. It is started by right clicking the test section and the Run Test in or Debug tests.
  1. package com.example.roboelectric  
  2. import org.juint.runner.RunWith  
  3. import org.roboelectric.RoboelectricTestRunner  
  4. import org.roboelectric.shadows.ShadowApplication  
  5. import android.content.Intent  
  6. import android.widgit.Button  
  7. import android.widgit.TextView  
  8. import org.junit.Test  
  9. import org.roboelectric.Roboelectric  
  10. import org.junit.Assert.*@RunWith(RoboelectricTestRunner::class)  
  11. class MainActivityTest {  
  12.     @Test  
  13.     fun clickingGo_ShouldWriteToTextView() {  
  14.         val activity = Roboelectric.setupActivity(MainActivity::class.java)  
  15.         activity.findViewById < Button > (R.id.go).  
  16.         performClick()  
  17.         assertEquals("Clicked", activity.findViewById < TextView > (R.id.tv).text)  
  18.     }  
  19. }   

Unit Test with Mocking

The test hook into the call of Android operating system function and simulate the execution is mocking. To include the mocking in the unit test, use the mockito test library or use the PowerMock. The below code is used to enable PowerMock in the application build.gradle file.
  1. anndroid {  
  2.     testOptions {  
  3.         unitTests.returnDefaultValues = true  
  4.     }  
  5. }  
  6. dependencies {  
  7.     testImplementation('org.powermock : powermock-mockito release-full : 1.6.1') {  
  8.         exclude module: 'hamcrest-core'  
  9.         exclude module: 'objenesis'  
  10.     }  
  11.     testImplementation ' org.reflections : relections:0.9.11'  
  12. }  
  13. }   

Integration Tests

The integration test sits between a unit test, and works on the development machine and a fully-fledged user interface test running on real or virtual devices and it tests the selected components in an isolated execution environment.
Integration tests happen inside the androidTest section of source code. Add a couple of packages to the build.gradle file.
The integration test is defined by a separate gradle source code and it runs separately from the unit test.
It must be possible to run only unit or integration tests.  
  1. dependencies      
  2. {      
  3.  androidTestImplementation      
  4.          ' com.android.support : support-annotations : 27.1.1'      
  5.  androidTestImplementation      
  6.          ' com.android.support.test : runner : 1.0.2'      
  7.  androidTestIMplementation      
  8.          ' com.android.support.test : 1.0.2'      
  9. }     

Broadcast Services

The Android testing framework does not pay attention to the broadcast services. 
  • Expresso 
    Use expresso to write tests targeting the application, disregarding any interapp activities. 

  • UI Automator
    Use UI Automator to write tests that span applications. It is used to inspect layouts to find the Ui structure of activities and checks on UI elements.