
It’s awesome to have you right here for another session that will completely change your perception about Unit Testing!!!
Nowadays, several developers perceive unit tests to be strenuous, laborious, redundant, needless, and monotonous. And if you were to look at the “unit tests” of these developers, you would promptly agree.
The trouble is that these perceptions (and the invalid tests that inspire them) derive from a common misconception of the reason for unit tests.
Unit testing was never an interesting concept to me at my early stage of software development. I saw everything as a waste of time. It was pretty difficult to adapt, however I scaled through.
Basically, a unit test is a test of a program’s result. When that program is a pure function, writing a suitable test is insignificant: You call the program, passing to it some combination of arguments, and verify that the return value correctly corresponds with those arguments. In other words, you give the function some input and verify the output.
https://gist.github.com/Temidtech/43e3966e226ece9202867730cc0ecfcb
I trust you now have an in-depth of what unit testing is all about…
Beautiful!!
In this article, I will provide you with all you need to setup unit testing on android studio…
Local Unit Testing on Android Studio
If your unit test has no dependencies or only has simple dependencies on Android, you should run your test on a local development machine. This testing approach is efficient because it helps you avoid the overhead of loading the target app and unit test code onto a physical device or emulator every time your test is run. Consequently, the execution time for running your unit test is greatly reduced. With this approach, you normally use a mocking framework, like Mockito, to fulfill any dependency relationships.
Set up your testing environment
Okay, let’s go to your Android Studio project, you must save the source files for local unit tests at module-name/src/test/java/
. By default, this directory exists when you create a new project.
In addition, you need to configure the testing dependencies for your project to use the standard APIs provided by the JUnit 4 framework. If your test needs to interact with Android dependencies, include the Mockito library to simplify your local unit tests. To learn more about using mock objects in your local unit tests, read my next article I have demystified it for you.
In your app’s top-level build.gradle
file, you need to specify these libraries as dependencies:
dependencies { // Required -- JUnit 4 framework testImplementation 'junit:junit:4.12' // Optional -- Mockito framework testImplementation 'org.mockito:mockito-core:1.10.19' }
Fantastic!!!
My next article talks about a simple local unit test for a Login system.
Are you ready?
No Comment! Be the first one.