Junit and Mockito Unit Testing Procedure
Junit and Mockito Unit Testing Procedure
1)Integration testing(AndroidTest):
• In Integration testing process where individual units are combined and tested
as group.
• The purpose of level of testing is to expose faults in the integration between
integrated units.
2)What is UnitTest(Test):
• This type of Testing performed by developer before the setup is handed over
the testing team to formally execute the test cases.
• It performed on the individual units of source code assigned areas.
• It is level of software testing process where individual units/components of a
software/system are tested.
• The goal of unit testing is to isolate each part of the program and show the
individual part are correct in term of requirements and functionality.
• After having exhausted all the point, there is no choice but to stop unit
testing and merge the code segment with other unit.
Dependencies Required for Unit Testing:
Junit :
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.13.2'
androidTestImplementation'androidx.test.ext:junit:1. 1.3'
testImplementation "com.google.truth:truth:1.1.3"
androidTestImplementation"com.google.truth:truth:1. 1.3"
➢ These four dependencies required for Junit Testing but Android
Studio already given Junit and androidx dependencies we need to add
one more dependencies that is google truth
➢ When we are Testing Junit first create class and right click then Select
Test for generate a Test Classes it will show Two Unit Test .
1)AndroidTest 2) Test
➢ We have to select Test for Junit Testing, after selecting Test It will
create Test Classes
➢ In the Test classes we can write our Test function for executing test
cases
<uses-library android:name="android.test.runner"
android:required="false" />
What is Mockito?
Mockito is a mocking framework, JAVA-based library that is used for
effective unit testing of JAVA applications. Mockito is used to mock
interfaces so that a dummy functionality can be added to a mock interface
that can be used in unit testing.
Mockito dependencies :
// Optional -- Mockito framework
testImplementation "org.mockito:mockito-core:4.5.1"
➢ When we need to add Mockito framework library first add the above
dependencies in build.gradle directory file
➢ @RunWith(MockitoJUnitRunner.class)
When we are Testing with(@RunWith(MockitoJUnitRunner.class))
Mockito these annotation are required to add before Test class
➢ One more Annotation required inside Test Class
➢ That is @Mock annotation where we can write our method or object
and call the object in the Test cases
Example:
Public class Email{
public static boolean isValidEmail(String email)
{
Pattern pattern;
Matcher matcher;
final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-
Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}
}
//Test class
Public class EmailTest{
@Test
public void isValidEmail() {
// email ="[email protected]";
assertTrue(StringUtil.isValidEmail(email));
}
}