While working on a code we intend to make it functional. But it is equally important that we make sure our code works the way we intended it to. And we can achieve that by witting unit test cases. In this blog, we will cover the same by considering two testing scenarios. First, where the method is independent. Second, where it is dependent on some other method or service. And in such cases, we perform the unit testing using the Mockito Framework.
What is Unit Testing?
UNIT TESTING is testing the individual units or components of a software program. The purpose behind performing Unit Testing is to validate whether each unit of our code is performing as expected. In most programming languages, that is a function, a method, or a property. Developer performs Unit Testing during the development phase of an application. JUnit, Mockito are some of the modern versions of unit testing.
What is Mocking?
We use mocking in unit testing. In simple English, Mocking is to make a replica of something. While testing an object/class, it may happen that a particular it may have dependencies on other complex objects. And since we want to test the methods/functions in isolation. We mock the services and methods from other classes and simulate the real behaviour of them using some mocking frameworks. And then we use those mocked methods/services to do unit testing in isolation. Mocking frameworks helps our tests to run more quickly and more reliably.
Now let us begin with an example on how to perform unit testing using mockito :-
To perform testing on our program we have used two library dependencies scalatest and mockito-scala. Which we have added to our build.sbt file as shown below. For its ability to support a number of different testing styles, we have used scalatest dependency. And have used mockito-scala dependency which helps to perform unit testing by mocking the dependent methods/services.
Below is the Person case class which takes three parameters name, age, vaccineStatus.
Below is the PersonAge class containing a method personIsSeniorCitizen() which checks whether a person is a senior citizen or not. If the age passed is above 45 years, it will return true else false.
Below is the VaccineStatus class containing a method personIsVaccinted() which checks whether a person is vaccinated or not. Checks whether status is yes or no. Will return true if found yes, else false.
Below is the PersonValidity class which checks whether a person is valid or not. A person is valid if he/she is a senior citizen and is vaccinated. So this class is dependent on two different classes PersonAge class and VaccineStatus Class. But here we are only concerned with PersonValidity class and not on its dependent methods (personIsSeniorCitizen() and personIsVaccinted()). So we will ignore the implementation of both the methods by creating a mock object and providing a dummy output.
In PersonAgeTest class first, we will import scalatest.flatspec dependency. And because the PersonAge class was independent of the other class/object. We will not be using the mockito framework to perform the test. Instead, we will directly use the assert() function to check if our test passes or fails. It matches the Actual output to the Expected output and then declares the test result.
As we can see below we have imported two dependencies which are scalatest.flatspec and mockito.MockitoSugar. Using mock we have mocked the PersonAge class and VaccineStatus class. And have provided the dummy output by using the when() method. If we change the age of a Person or its vaccine status. It will not affect the testing of PersonValidity class. As we are not concerned with the implementation of the PersonAge class and VaccineStatus class. In this way, we can check our code in small-small units. Applying unit testing help to debug the code faster. And to move to the exact unit which is failing.
Link to Best Practices: https://www.testim.io/blog/unit-testing-best-practices/
Link to Unit testing using Mockito Blog: https://blog.knoldus.com/unit-testing-with-mockito-framework/