0% found this document useful (0 votes)
35 views

Junit and Mockito Unit Testing Procedure

The document outlines the procedures for unit testing using JUnit and Mockito, highlighting the differences between integration testing and unit testing. It details the required dependencies for JUnit testing and provides an overview of Mockito as a mocking framework for effective unit testing. Additionally, it includes examples of test method annotations and a sample test class for validating email addresses.

Uploaded by

abinash.gadsara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Junit and Mockito Unit Testing Procedure

The document outlines the procedures for unit testing using JUnit and Mockito, highlighting the differences between integration testing and unit testing. It details the required dependencies for JUnit testing and provides an overview of Mockito as a mocking framework for effective unit testing. Additionally, it includes examples of test method annotations and a sample test class for validating email addresses.

Uploaded by

abinash.gadsara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Junit and Mockito Unit Testing Procedure

There are Two type of unit Testing is their


1-Integration testing (AndroidTest)
2-UnitTest(Test)

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

<!-- You don't need to include


android:required="false" if your app's

minSdkVersion is 28 or higher. -->

<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

Feature JUnit 4 Junit 5

Declare a test method @Test @Test

Execute before all test methods in the


@BeforeClass @BeforeAll
current class

Execute after all test methods in the current


@AfterClass @AfterAll
class

Execute before each test method @Before @BeforeEach

Execute after each test method @After @AfterEach

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));
}
}

You might also like