{"id":21673,"date":"2015-06-25T12:54:11","date_gmt":"2015-06-25T07:24:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=21673"},"modified":"2015-10-08T16:24:00","modified_gmt":"2015-10-08T10:54:00","slug":"mocking-static-methods-in-junit-using-powermock","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/mocking-static-methods-in-junit-using-powermock\/","title":{"rendered":"Mocking static methods in JUnit using PowerMock"},"content":{"rendered":"<p>We usually need\u00a0to mock lots of functionality while writing <a title=\"Node.js unit test\" href=\"http:\/\/www.tothenew.com\/blog\/node-js-unit-testing-with-jasmine-framework\/\" target=\"_blank\">unit tests<\/a>. In JUnit we have many frameworks to achieve this, but PowerMock is very powerfull API to mock classes. For mocking static functions we have a\u00a0<a title=\"Grails upgrade services \" href=\"http:\/\/www.tothenew.com\/grails-application-development\" target=\"_blank\">bit different approach<\/a> due to their different nature. Assuming we have two utility classes with static functions and one class for which we need to write unit test case.<\/p>\n<p><strong>FirstUtility class:<\/strong><\/p>\n<p>[java]<\/p>\n<p>public class FirstUtil {<\/p>\n<p>\tpublic static String staticFunction(String parameter){<br \/>\n\t\treturn parameter;<br \/>\n\t}<\/p>\n<p>}<\/p>\n<p>[\/java]<\/p>\n<p><strong>SecondUtility class:<\/strong><\/p>\n<p>[java]<\/p>\n<p>public class SecondUtil {<\/p>\n<p>\tpublic static String staticFunction(String parameter){<br \/>\n        return parameter;<br \/>\n\t}<\/p>\n<p>}<\/p>\n<p>[\/java]<\/p>\n<p><strong>Class for Unit Test:<\/strong><\/p>\n<p>[java]<br \/>\npublic class ForTest {<br \/>\n\tpublic String executeFunction(){<br \/>\n\t\tFirstUtil.staticFunction(&quot;FirstUtil&quot;);<br \/>\n\t\tSecondUtil.staticFunction(&quot;SecondUtil&quot;);<br \/>\n\t\treturn &quot;success&quot;;<br \/>\n\t}<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>For the above code we need to mock two classes. So we need to include them in annotation &#8220;PrepareForTest&#8221;.<\/p>\n<p>[java]<\/p>\n<p>@PowerMockIgnore(&quot;org.apache.http.conn.ssl.*&quot;)<br \/>\n@RunWith(PowerMockRunner.class)<br \/>\n@PrepareForTest({FirstUtil.class,SecondUtil.class})<\/p>\n<p>public class TestToMockStatic {<\/p>\n<p>\t@Rule<\/p>\n<p>\t@Test<br \/>\n\t    public void testProductOrderProcessing() throws ClientProtocolException, IOException{<br \/>\n\t    \tPowerMockito.mockStatic(FirstUtil.class);\/\/For mocking static functions<br \/>\n\t    \tPowerMockito.mockStatic(SecondUtil.class);<\/p>\n<p>\t    \tMockito.when(FirstUtil.staticFunction(parameter)).thenReturn(mockedStaticFunction(parameter));<br \/>\n\t    \tMockito.when(SecondUtil.staticFunction(parameter)).thenReturn(mockedStaticFunction(parameter));<\/p>\n<p>\t    \tForTest forTest= new ForTest();<\/p>\n<p>\t    \tassertEquals(forTest.executeFunction(),&quot;success&quot;);<\/p>\n<p>\t    }<\/p>\n<p>\t    public void mockedStaticFunction(String parameter){<br \/>\n\t    \treturn parameter;<br \/>\n\t    }<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Note :<\/strong> In above code you can see annotation @PowerMockIgnore . This is used to defer the loading of classes with the names supplied to value() to the system classloader.<\/p>\n<p>Maven dependencies for mocking framework:<\/p>\n<p>&nbsp;<\/p>\n<p>[xml]<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.powermock&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;powermock-core&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;1.6.2&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<\/p>\n<p>        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.powermock&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;powermock-api-support&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;1.6.2&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<\/p>\n<p>        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.powermock&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;powermock-module-junit4&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;${powermock.version}&lt;\/version&gt;<br \/>\n            &lt;scope&gt;test&lt;\/scope&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.powermock&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;powermock-api-mockito&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;${powermock.version}&lt;\/version&gt;<br \/>\n            &lt;scope&gt;test&lt;\/scope&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.mockito&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;mockito-core&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;1.8.5&lt;\/version&gt;<br \/>\n            &lt;scope&gt;test&lt;\/scope&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n[\/xml]<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We usually need\u00a0to mock lots of functionality while writing unit tests. In JUnit we have many frameworks to achieve this, but PowerMock is very powerfull API to mock classes. For mocking static functions we have a\u00a0bit different approach due to their different nature. Assuming we have two utility classes with static functions and one class [&hellip;]<\/p>\n","protected":false},"author":226,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":11},"categories":[1818,446,1,1816],"tags":[1913,1912,1914,272],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21673"}],"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\/226"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=21673"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21673\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=21673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=21673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=21673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}