{"id":4388,"date":"2021-06-30T14:55:55","date_gmt":"2021-06-30T14:55:55","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4388"},"modified":"2021-07-03T10:34:10","modified_gmt":"2021-07-03T10:34:10","slug":"how-to-test-private-method-using-mockito","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-to-test-private-method-using-mockito\/","title":{"rendered":"How to Test private method using mockito"},"content":{"rendered":"<p>Below we have a Student class with Private Class Variable(id and name), Service and Private Methods.<\/p>\n<p><strong>Student.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Student {\r\n    private Integer id;\r\n    private String name;\r\n\r\n    @Autowired\r\n    private StudentService studentService;\r\n\r\n    private String getStudentDetails(){\r\n      return &quot;id: &quot; + getId() + &quot;; name: &quot; + getName();\r\n    }  \r\n}\r\n<\/pre>\n<p><strong class=\"ctaHeader2\">Mocking a Private Field<\/strong><br \/>\nwe cannot access the private field id to assign a value for testing, because <strong>there isn&#8217;t a public setter method<\/strong> for it.<br \/>\nWe can then use ReflectionTestUtils.setField method to assign a value to the private member id<\/p>\n<p><strong>StudentTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Test\r\npublic void setValueWithNoSetter() {\r\n    Student student = new Student();\r\n    ReflectionTestUtils.setField(student, &quot;id&quot;, 101); \r\n    assertTrue(student.getId().equals(101));\r\n}\r\n<\/pre>\n<p><strong class=\"ctaHeader2\">Mocking a Private Method<\/strong><br \/>\nIn a similar way we can invoke private method &#8211; getStudentDetails() as below in student class<br \/>\n<strong>StudentTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Test\r\npublic void whenNonPublicMethod_thenReflectionTestUtilsInvokeMethod() {\r\n    Student student= new Student ();\r\n    ReflectionTestUtils.setField(student, &quot;id&quot;, 101);\r\n    ReflectionTestUtils.setField(student, &quot;name&quot;, &quot;Mugil&quot;);\r\n \r\n    assertTrue(ReflectionTestUtils.invokeMethod(student, &quot;employeeToString&quot;).equals\r\n                 (&quot;id: 101; name: Mugil&quot;));\r\n}\r\n<\/pre>\n<p><strong class=\"ctaHeader2\">Mocking a Private Dependencies<\/strong><br \/>\n<strong>StudentTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Test\r\npublic void whenNonPublicMethod_thenReflectionTestUtilsInvokeMethod() \r\n{\r\n   Student student = new Student();\r\n   StudentService studService = mock(StudentService.class);\r\n   when(studService.getStudentStatus(student.getId())).thenReturn(&quot;Active&quot;);\r\n\r\n   ReflectionTestUtils.setField(student, &quot;studentService&quot;, studService);\r\n\r\n.\r\n.\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below we have a Student class with Private Class Variable(id and name), Service and Private Methods. Student.java public class Student { private Integer id; private String name; @Autowired private StudentService studentService; private String getStudentDetails(){ return &quot;id: &quot; + getId() + &quot;; name: &quot; + getName(); } } Mocking a Private Field we cannot access the&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-to-test-private-method-using-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":[324],"tags":[],"class_list":["post-4388","post","type-post","status-publish","format-standard","hentry","category-mockito"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4388","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=4388"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4388\/revisions"}],"predecessor-version":[{"id":4396,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4388\/revisions\/4396"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}