{"id":3913,"date":"2020-09-17T15:43:54","date_gmt":"2020-09-17T15:43:54","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=3913"},"modified":"2020-09-17T15:56:38","modified_gmt":"2020-09-17T15:56:38","slug":"stub-vs-mockito","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/stub-vs-mockito\/","title":{"rendered":"Stub vs Mockito"},"content":{"rendered":"<p>Below is a simple class which uses stub for Testing<\/p>\n<ol>\n<li>Below is a simple business implementation class which <strong>EmployeeDetailsImpl<\/strong> which uses <strong>EmployeeService<\/strong> API methods <\/li>\n<li>Now to test EmployeeDetailsImpl we need to substitude the methods in service class with stub methods for which we create <strong>EmployeeServiceStub<\/strong><\/li>\n<li><strong>EmployeeServiceStub<\/strong> is a stub class which implements the <strong>EmployeeService <\/strong>and provide method definitions<\/li>\n<li><strong>EmployeeDetailsImplTest<\/strong> is the test class for EmployeeDetailsImpl which performs the unittest for methods in EmployeeDetailsImpl<\/li>\n<\/ol>\n<p><strong>EmployeeService.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\nimport java.util.List;\r\n\r\npublic interface EmployeeService {\r\n    public List&lt;String&gt; GetEmployeeDetails();\r\n}\r\n<\/pre>\n<p><strong>EmployeeDetailsImpl.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\nimport com.mugil.org.api.EmployeeService;\r\n\r\nimport java.util.List;\r\n\r\npublic class EmployeeDetailsImpl {\r\n    EmployeeService employeeService;\r\n\r\n    public EmployeeDetailsImpl(EmployeeService pEmployeeService){\r\n        this.employeeService = pEmployeeService;\r\n    }\r\n\r\n    public List&lt;String&gt; getEmployeeList(){\r\n        List&lt;String&gt; arrEmp = this.employeeService.GetEmployeeDetails();\r\n        return arrEmp;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>EmployeeServiceStub.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\nimport java.util.Arrays;\r\nimport java.util.List;\r\n\r\npublic class EmployeeServiceStub  implements  EmployeeService{\r\n\r\n    @Override\r\n    public List&lt;String&gt; GetEmployeeDetails() {\r\n        return Arrays.asList(&quot;Mugil&quot;, &quot;Mani&quot;, &quot;Vinu&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>EmployeeDetailsImplTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\nimport com.mugil.org.api.EmployeeServiceStub;\r\nimport org.junit.Test;\r\n\r\nimport static org.junit.Assert.assertEquals;\r\n\r\npublic class EmployeeDetailsImplTest {\r\n    @Test\r\n    public void getEmployeeList_success(){\r\n        EmployeeServiceStub employeeService = new  EmployeeServiceStub();\r\n        EmployeeDetailsImpl employeeDetailsImpl = new EmployeeDetailsImpl(employeeService);\r\n        assertEquals(3, employeeDetailsImpl.getEmployeeList().size());\r\n    }\r\n}\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">Dis-advantage of the above implementation<\/strong><\/p>\n<ol>\n<li>In the above implementation when ever new method is added to the API interface, it should be added to the <strong>EmployeeServiceStub<\/strong> which implements  EmployeeService<\/li>\n<li><strong>EmployeeServiceStub<\/strong> is extra java file which should be taken care, incase there are multiple services used in class multiple service stub needs to be created<\/li>\n<\/ol>\n<p><strong class=\"ctaHeader3\">Replacing stub with mocks<\/strong><br \/>\nIn the below code, the same stub service(<strong>EmployeeServiceStub.java<\/strong>) is replaced with mock class and its methods are replaced using when and return.This prevents the need for another class <\/p>\n<p><strong>EmployeeDetailsImplTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\nimport static org.junit.Assert.assertEquals;\r\nimport static org.mockito.Mockito.mock;\r\nimport static org.mockito.Mockito.when;\r\n\r\npublic class EmployeeDetailsImplTest {\r\n    @Test\r\n    public void getEmployeeList_success(){\r\n     \r\n        \/\/Replacement code for Stub class\r\n        EmployeeService employeeService = mock(EmployeeService.class);\r\n        when(employeeService.GetEmployeeDetails()).thenReturn(Arrays.asList(&quot;Mugil&quot;, &quot;Mani&quot;, &quot;Vinu&quot;));\r\n\r\n        EmployeeDetailsImpl employeeDetailsImpl = new EmployeeDetailsImpl(employeeService);\r\n        assertEquals(3, employeeDetailsImpl.getEmployeeList().size());\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below is a simple class which uses stub for Testing Below is a simple business implementation class which EmployeeDetailsImpl which uses EmployeeService API methods Now to test EmployeeDetailsImpl we need to substitude the methods in service class with stub methods for which we create EmployeeServiceStub EmployeeServiceStub is a stub class which implements the EmployeeService and&hellip; <a href=\"https:\/\/codethataint.com\/blog\/stub-vs-mockito\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[323],"tags":[],"class_list":["post-3913","post","type-post","status-publish","format-standard","hentry","category-interview-testing"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3913"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3913\/revisions"}],"predecessor-version":[{"id":3920,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3913\/revisions\/3920"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}