{"id":55598,"date":"2022-09-29T15:25:59","date_gmt":"2022-09-29T09:55:59","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55598"},"modified":"2022-09-30T15:30:19","modified_gmt":"2022-09-30T10:00:19","slug":"unit-testing-using-junit-and-mockito","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/unit-testing-using-junit-and-mockito\/","title":{"rendered":"Unit Testing using JUnit and Mockito"},"content":{"rendered":"<h2 style=\"text-align: center;\"><b>Introduction to Unit testing<\/b><\/h2>\n<p style=\"text-align: left;\"><b>What is Unit testing?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Unit testing is a process that is used to verify a unit or unit\u2019s functionality. This step helps in the estimation of the codes by other team members and also creates a record of all necessary information including errors that occur. It provides an opportunity for the developer to fix errors before it becomes a problem with other modules. The code is exercised by making small modifications instead of adding many new lines in order to cover all scenarios and make sure that there are no loopholes in the program.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Unit Testing is the software testing process where a set of software program components(methods) or modules are tested individually. This technique effectively checks the implementation of the functionality and the accuracy of a section of code by using stubs, mock objects, dependencies, and unit testing frameworks.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: center;\"><b>Importance of Unit testing<\/b><\/h2>\n<p><b>\u00a0<\/b> <b>Why do we need\u00a0 Unit testing?<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Reducing bug fixing costs.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Validating and correcting the bugs in the early stages.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Makes the debugging process simple and smooth.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Safe code refactoring.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Improves the Quality of Code.<\/span><\/li>\n<\/ul>\n<p><b><br \/>\n<\/b> <b>Qualities of UTC and best practices while writing the UTCs \u00a0 :<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The code should be strong and loosely coupled.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Unit tests are repeatable and scalable.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Write isolated tests.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Unit tests should be automated.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Test One Scenario Per Test.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Write readable, simple Tests.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>\u201cTest-everything-that-can-possibly-break<\/b><span style=\"font-weight: 400;\">\u201d programming strategy.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Should follow <\/span><b>AAA<\/b><span style=\"font-weight: 400;\"> rule:<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><b>Arrange<\/b><span style=\"font-weight: 400;\">: Do all the setup(creating mock objects etc,) and configurations to test the method\/system.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Act<\/b><span style=\"font-weight: 400;\">:\u00a0 Execute the action to perform the test.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Assert<\/b><span style=\"font-weight: 400;\"> \u2013 Verify the functionality of the method\/system.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2 style=\"text-align: center;\"><b>Unit Testing Frameworks<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">UnitTest Framework is a testing methodology by which individual units of source code, such as class, methods, and functions are tested to determine whether they perform their functionality properly or not. It also includes code coverage and code quality and good coding practices.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">There are several UnitTest frameworks available as per the different programming languages. For example:<\/span><b><br \/>\n<\/b><\/p>\n<p><b>1. JUnit:<\/b><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Open-source unit testing framework for JAVA<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Supports the core concept of \u201c<\/span><b>first testing then coding<\/b><span style=\"font-weight: 400;\">\u201d<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Annotation-based unit tests.Ex : @Test, @Before, @after etc..<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Supports various build tools like ANT, Maven, etc.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b style=\"font-size: 1rem;\">2. TestNG<\/b><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">open-source automation testing framework for JAVA<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">designed for Java Programming language<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">supports concurrent testing and offers annotation support<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b>3. Embunit<\/b><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00a0an open source unit testing framework for C\/ C++<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b>4. HtmlUnit<\/b><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00a0open-source unit testing framework for HTML<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2 style=\"text-align: center;\"><b>JUnit\u00a0<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">\u00a0<\/span><b>JUnit<\/b><span style=\"font-weight: 400;\">: A unit testing framework that is extensively used to test the code written in JAVA. It is used to verify the implementation of the functionality of the modules and check whether the requirements have been met or not.<\/span><\/p>\n<p><b>Some salient features of JUnit :<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>Test setup and tearDown<\/b><span style=\"font-weight: 400;\">: Setup and teardown the context before and after running the test respectively.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Asserts<\/b><span style=\"font-weight: 400;\">: Specify the expected output and compare it with the received output.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Exception Testing<\/b><span style=\"font-weight: 400;\">: Tests and verifies whether an exception was thrown.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Test Suits<\/b><span style=\"font-weight: 400;\">: JUnit test cases are organized into test suites for better management.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Integration with popular build Systems<\/b><span style=\"font-weight: 400;\">:\u00a0 Integrates with the most popular build systems for java, including ANT and Maven.<\/span><\/li>\n<\/ul>\n<p><b>Some frequently used annotations and Assert statements\u00a0 in JUnit :<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>@Test<\/b><span style=\"font-weight: 400;\">: This is the test method to run, <\/span><i><span style=\"font-weight: 400;\">Return Type: public void<\/span><\/i><\/li>\n<li style=\"font-weight: 400;\"><b>@Before<\/b><span style=\"font-weight: 400;\">: Run before @Test, <\/span><i><span style=\"font-weight: 400;\">Return Type:\u00a0 public void<\/span><\/i><\/li>\n<li style=\"font-weight: 400;\"><b>@After<\/b><span style=\"font-weight: 400;\">: Run after @Test, <\/span><i><span style=\"font-weight: 400;\">Return Type:\u00a0 public void<\/span><\/i><\/li>\n<li style=\"font-weight: 400;\"><b>@BeforeClass<\/b><span style=\"font-weight: 400;\">: Run once before any of the test methods in the class, <\/span><i><span style=\"font-weight: 400;\">Return Type:\u00a0 public <\/span><\/i><span style=\"font-weight: 400;\">static void<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>@AfterClass<\/b><span style=\"font-weight: 400;\">: Run once after all the tests in the class have been run, <\/span><i><span style=\"font-weight: 400;\">Return Type: public static void<\/span><\/i><\/li>\n<li style=\"font-weight: 400;\"><b>@Ignore: <\/b><span style=\"font-weight: 400;\">Blocks the test case execution.\u00a0<\/span><\/li>\n<\/ul>\n<p><b>Sample Test Class :<\/b><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-55595\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-18-59-05.png\" alt=\"\" width=\"980\" height=\"768\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-18-59-05.png 980w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-18-59-05-300x235.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-18-59-05-768x602.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-18-59-05-624x489.png 624w\" sizes=\"(max-width: 980px) 100vw, 980px\" \/><\/p>\n<p><b>Output :<\/b><\/p>\n<div id=\"attachment_55596\" style=\"width: 1034px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-55596\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-55596 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-19-13-35.png\" alt=\"\" width=\"1024\" height=\"261\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-19-13-35.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-19-13-35-300x76.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-19-13-35-768x196.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-19-13-35-624x159.png 624w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><p id=\"caption-attachment-55596\" class=\"wp-caption-text\">The output of test case<\/p><\/div>\n<h2 style=\"text-align: center;\"><b>Mockito<\/b><\/h2>\n<p><b>What is Mockito?<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">An open source mocking framework.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Uses mock testing strategy, uses substitutes of objects to validate the code.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Simple coding language.<\/span><\/li>\n<\/ul>\n<p><b>What is Mocking?\u00a0\u00a0\u00a0<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Based on the concept of a mock object (dummy object) that replaces the real objects.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0Tests the objects and methods with dependencies.<\/span><\/li>\n<\/ul>\n<p><b>What are the different phases of Mocking?<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Stubbing:\u00a0 It specifies how the object will behave when the object is involved in the interaction.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Expectation : Set an object such that it tells the expectations when the test is executed.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Verification: Confirms that expectations have been met.<\/span><\/li>\n<\/ul>\n<p><b>How to implement the Mockito in JUnit?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">There are three steps to implement the\u00a0 Mockito in JUnit and these steps are as follows:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Creating mock object\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Using stubs to set the expectations.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Verifying using mockito.<\/span><\/li>\n<\/ul>\n<p><b>1. Creating mock object\u00a0 :<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>\u00a0<\/b><span style=\"font-weight: 400;\">Create mock objects in JUnit by Using:<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Static <\/span><b><i>mock() <\/i><\/b><span style=\"font-weight: 400;\">method call<\/span><\/li>\n<li style=\"font-weight: 400;\"><b><i>@Mock<\/i><\/b><span style=\"font-weight: 400;\"> annotation<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00a0A mock object is created and used to test the service<\/span><\/li>\n<\/ul>\n<p><b>2. Using stubs to set the expectations.<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Create a stub and pass an argument to the method as a mock.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Use the anyInt(), any(), anyString () respectively as per argument needed.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">For stubbing Void method calls :<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><em><strong>When()<\/strong><\/em>\u00a0 method cannot be used.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Use <\/span><b><i>doReturn(result).when(mock_Object).void_method_Call()<\/i><\/b><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Exceptions using<\/span> <b><i>thenThrow() or doThrow()<\/i><\/b><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b>3. <\/b><b>Verifying using mockito.<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Use <\/span><b><i>verify()<\/i><\/b><span style=\"font-weight: 400;\"> method to check whether the set expectations are met.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Verify can check the behavior of the method like method invocations happened twice\/once, multiple times or never.<\/span><\/li>\n<\/ul>\n<p><b>Sample test case implementing Mockito in JUnit:\u00a0<\/b><\/p>\n<div id=\"attachment_55597\" style=\"width: 850px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-55597\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-55597\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-20-24-29.png\" alt=\"\" width=\"840\" height=\"702\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-20-24-29.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-20-24-29-300x251.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-28-20-24-29-624x522.png 624w\" sizes=\"(max-width: 840px) 100vw, 840px\" \/><p id=\"caption-attachment-55597\" class=\"wp-caption-text\">Implementation of\u00a0 Mockito in JUnit<\/p><\/div>\n<p><span style=\"font-weight: 400;\">Some important Annotations of Mockito:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\"><strong>@spy:<\/strong> P<\/span><span style=\"font-weight: 400;\">artial mock, which will track the interactions with the object like a mock.<\/span><\/li>\n<li><strong>@Mock: <\/strong><span style=\"font-weight: 400;\">create a mock object of a class or an interface.<\/span><\/li>\n<li><strong>@InjectMock: <\/strong><span style=\"font-weight: 400;\">inject mocked dependencies in the annotated class mocked object.<\/span><\/li>\n<\/ul>\n<p><b>Assert Class:<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\">It is a JUnit class that consists of a set of assertion methods useful for writing tests. Only failed assertions are recorded. These methods can be used directly.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Example :<\/span><\/p>\n<p><b>assertArrayEquals(boolean[] expected, boolean[] actuals) : <\/b><span style=\"font-weight: 400;\">Asserts that two boolean arrays are equal.<\/span><\/p>\n<p><b>assertArrayEquals(byte[] expected, byte[] actuals) :\u00a0 <\/b><span style=\"font-weight: 400;\">Asserts that two byte arrays are equal.<\/span><\/p>\n<p><b>assertArrayEquals(char[] expected, char[] actuals) :\u00a0 <\/b><span style=\"font-weight: 400;\">Asserts that two char arrays are equal.<\/span><\/p>\n<p><b>assertNotNull(Object object) : <\/b><span style=\"font-weight: 400;\">Asserts that an object isn&#8217;t null.<\/span><\/p>\n<p><b>For more such assert methods: <\/b><a href=\"https:\/\/junit.org\/junit4\/javadoc\/4.13\/org\/junit\/Assert.html\"><b>https:\/\/junit.org\/junit4\/javadoc\/4.13\/org\/junit\/Assert.html<\/b><\/a><\/p>\n<h2 style=\"text-align: center;\"><strong>References<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Wikipedia, tutorialspoint, javatpoint, junit.org<\/span><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Introduction to Unit testing What is Unit testing? Unit testing is a process that is used to verify a unit or unit\u2019s functionality. This step helps in the estimation of the codes by other team members and also creates a record of all necessary information including errors that occur. It provides an opportunity for the [&hellip;]<\/p>\n","protected":false},"author":1497,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":40},"categories":[446,1994,1816],"tags":[5029],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55598"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1497"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55598"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55598\/revisions"}],"predecessor-version":[{"id":55644,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55598\/revisions\/55644"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}