{"id":3921,"date":"2020-09-17T16:38:01","date_gmt":"2020-09-17T16:38:01","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=3921"},"modified":"2021-02-13T07:17:05","modified_gmt":"2021-02-13T07:17:05","slug":"mocking-list-methods","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/mocking-list-methods\/","title":{"rendered":"Mockito Basics"},"content":{"rendered":"<p>In the below code we are going to mock the List Interface and override the method behaviour of List Methods<br \/>\nMethods mocked<\/p>\n<ol>\n<li>get(index)<\/li>\n<li>size()<\/li>\n<li>exception<\/li>\n<\/ol>\n<p><strong>ListTest.java<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.org;\r\n\r\nimport org.junit.Before;\r\nimport org.junit.Test;\r\nimport java.util.List;\r\nimport static org.junit.Assert.assertNotEquals;\r\nimport static org.mockito.Matchers.anyInt;\r\nimport static org.mockito.Mockito.mock;\r\nimport static org.mockito.Mockito.when;\r\nimport static org.junit.Assert.assertEquals;\r\n\r\npublic class ListTest {\r\n    List arrEmployee;\r\n\r\n    @Before\r\n    public void init(){\r\n        arrEmployee = mock(List.class);\r\n    }\r\n\r\n    @Test\r\n    public void ListMock_SizeMethod(){\r\n        when(arrEmployee.size()).thenReturn(3);\r\n        assertEquals(3, arrEmployee.size());\r\n    }\r\n\r\n    @Test\r\n    public void ListMock_GetMethod(){\r\n        when(arrEmployee.get(0)).thenReturn(&quot;Employee1&quot;);\r\n        when(arrEmployee.get(1)).thenReturn(&quot;Employee2&quot;);\r\n        when(arrEmployee.get(2)).thenReturn(&quot;Employee3&quot;);\r\n\r\n        assertEquals(&quot;Employee2&quot;, arrEmployee.get(1));\r\n        assertNotEquals(null, arrEmployee.get(2));\r\n    }\r\n\r\n    @Test(expected = RuntimeException.class)\r\n    public void ListMock_ThrowException(){\r\n        when(arrEmployee.get(anyInt())).thenThrow(new RuntimeException());\r\n        arrEmployee.get(1);\r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the below code we are going to mock the List Interface and override the method behaviour of List Methods Methods mocked get(index) size() exception ListTest.java package com.mugil.org; import org.junit.Before; import org.junit.Test; import java.util.List; import static org.junit.Assert.assertNotEquals; import static org.mockito.Matchers.anyInt; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.junit.Assert.assertEquals; public class ListTest { List arrEmployee;&hellip; <a href=\"https:\/\/codethataint.com\/blog\/mocking-list-methods\/\">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":[324],"tags":[],"class_list":["post-3921","post","type-post","status-publish","format-standard","hentry","category-mockito"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3921","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=3921"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3921\/revisions"}],"predecessor-version":[{"id":4154,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3921\/revisions\/4154"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}