{"id":991,"date":"2022-07-07T01:01:28","date_gmt":"2023-10-17T11:51:24","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/mockito-is-used-to-mock-static-methods-in-java-while-powermock-is-another-tool-that-can-be-utilized-for-the-same-purpose\/"},"modified":"2025-07-18T14:42:09","modified_gmt":"2025-07-18T14:42:09","slug":"powermock-and-mockito-in-java","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/","title":{"rendered":"PowerMock and Mockito are used to mock static methods in Java"},"content":{"rendered":"<p>Mockito enables the creation of mock objects. As static methods are associated with the class, Mockito does not offer the capability to mock them. Nevertheless, it is possible to utilize the PowerMock framework in conjunction with Mockito to mock static methods.<\/p>\n<h2>Use PowerMock to create a Mockito mock for a static method.<\/h2>\n<p>PowerMock offers various modules to enhance the capabilities of the Mockito framework and execute JUnit and TestNG test cases. It should be noted that PowerMock does not currently support JUnit 5, hence we will be creating JUnit 4 test cases. Furthermore, we will comprehend the integration of TestNG with Mockito and PowerMock.<\/p>\n<h2>Dependencies required for PowerMock<\/h2>\n<p>For mocking static methods in Mockito, we require the PowerMock dependencies mentioned below.<\/p>\n<ul class=\"post-ul\">\n<li>powermock-api-mockito2: This is the core PowerMock dependency and used to extend Mockito2 mocking framework. If you are using Mockito 1.x versions then use powermock-api-mockito module.<\/li>\n<li>powermock-module-junit4: For running JUnit 4 test cases using PowerMock.<\/li>\n<li>powermock-module-testng: For running TestNG test cases and supporting PowerMock.<\/li>\n<\/ul>\n<p>Here is the last pom.xml file of our project.<\/p>\n<pre class=\"post-pre\"><code>&lt;project xmlns=\"https:\/\/maven.apache.org\/POM\/4.0.0\"\r\n\txmlns:xsi=\"https:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n\txsi:schemaLocation=\"https:\/\/maven.apache.org\/POM\/4.0.0 https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\t&lt;groupId&gt;com.scdev.powermock&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;PowerMock-Examples&lt;\/artifactId&gt;\r\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n\r\n\t&lt;properties&gt;\r\n\t\t&lt;testng.version&gt;6.14.3&lt;\/testng.version&gt;\r\n\t\t&lt;junit4.version&gt;4.12&lt;\/junit4.version&gt;\r\n\t\t&lt;mockito-core.version&gt;2.19.0&lt;\/mockito-core.version&gt;\r\n\t\t&lt;powermock.version&gt;2.0.0-beta.5&lt;\/powermock.version&gt;\r\n\t\t&lt;java.version&gt;10&lt;\/java.version&gt;\r\n\t&lt;\/properties&gt;\r\n\r\n\t&lt;dependencies&gt;\r\n\t\t&lt;!-- TestNG --&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.testng&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;testng&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;${testng.version}&lt;\/version&gt;\r\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t\t&lt;!-- JUnit 4 --&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;junit&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;junit&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;${junit4.version}&lt;\/version&gt;\r\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t\t&lt;!-- Mockito 2 --&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.mockito&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;mockito-core&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;${mockito-core.version}&lt;\/version&gt;\r\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t\t&lt;!-- PowerMock TestNG Module --&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.powermock&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;powermock-module-testng&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;${powermock.version}&lt;\/version&gt;\r\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t\t&lt;!-- PowerMock JUnit 4.4+ Module --&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.powermock&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;powermock-module-junit4&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;${powermock.version}&lt;\/version&gt;\r\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t\t&lt;!-- PowerMock Mockito2 API --&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.powermock&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;powermock-api-mockito2&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;${powermock.version}&lt;\/version&gt;\r\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t&lt;\/dependencies&gt;\r\n\t&lt;build&gt;\r\n\t\t&lt;plugins&gt;\r\n\t\t\t&lt;plugin&gt;\r\n\t\t\t\t&lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\r\n\t\t\t\t&lt;version&gt;3.7.0&lt;\/version&gt;\r\n\t\t\t\t&lt;configuration&gt;\r\n\t\t\t\t\t&lt;source&gt;${java.version}&lt;\/source&gt;\r\n\t\t\t\t\t&lt;target&gt;${java.version}&lt;\/target&gt;\r\n\t\t\t\t&lt;\/configuration&gt;\r\n\t\t\t&lt;\/plugin&gt;\r\n\t\t\t&lt;plugin&gt;\r\n\t\t\t\t&lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n\t\t\t\t&lt;artifactId&gt;maven-surefire-plugin&lt;\/artifactId&gt;\r\n\t\t\t\t&lt;version&gt;2.22.0&lt;\/version&gt;\r\n\t\t\t\t&lt;dependencies&gt;\r\n\t\t\t\t\t&lt;dependency&gt;\r\n\t\t\t\t\t\t&lt;groupId&gt;org.apache.maven.surefire&lt;\/groupId&gt;\r\n\t\t\t\t\t\t&lt;artifactId&gt;surefire-junit47&lt;\/artifactId&gt;\r\n\t\t\t\t\t\t&lt;version&gt;2.22.0&lt;\/version&gt;\r\n\t\t\t\t\t&lt;\/dependency&gt;\r\n\t\t\t\t\t&lt;dependency&gt;\r\n\t\t\t\t\t\t&lt;groupId&gt;org.apache.maven.surefire&lt;\/groupId&gt;\r\n\t\t\t\t\t\t&lt;artifactId&gt;surefire-testng&lt;\/artifactId&gt;\r\n\t\t\t\t\t\t&lt;version&gt;2.22.0&lt;\/version&gt;\r\n\t\t\t\t\t&lt;\/dependency&gt;\r\n\t\t\t\t&lt;\/dependencies&gt;\r\n\t\t\t\t&lt;configuration&gt;\r\n\t\t\t\t\t&lt;additionalClasspathElements&gt;\r\n\t\t\t\t\t\t&lt;additionalClasspathElement&gt;src\/test\/java\/&lt;\/additionalClasspathElement&gt;\r\n\t\t\t\t\t&lt;\/additionalClasspathElements&gt;\r\n\t\t\t\t\t&lt;!-- TestNG Test Fails when executed from command line with message\r\n\t\t\t\t\t\t\"Cannot use a threadCount parameter less than 1\" \r\n\t\t\t\t\t\tWorks when threadCount is explicitly specified \r\n\t\t\t\t\t\thttps:\/\/gist.github.com\/juherr\/6eb3e93e2db33979b7e90b63ddadc888--&gt;\r\n\t\t\t\t\t&lt;threadCount&gt;5&lt;\/threadCount&gt;\r\n\t\t\t\t&lt;\/configuration&gt;\r\n\t\t\t&lt;\/plugin&gt;\r\n\t\t&lt;\/plugins&gt;\r\n\t&lt;\/build&gt;\r\n&lt;\/project&gt;\r\n<\/code><\/pre>\n<p>Please note that I am currently using the PowerMock version 2.0.0-beta.5. This particular version is compatible with Java 10, although it is still in the beta stage, meaning there may be some problems in more complicated scenarios. When I attempted to use the stable version 1.7.x, I encountered the following error messages.<\/p>\n<pre class=\"post-pre\"><code>java.lang.NoSuchMethodError: org.mockito.internal.handler.MockHandlerFactory.createMockHandler(Lorg\/mockito\/mock\/MockCreationSettings;)Lorg\/mockito\/internal\/InternalMockHandler;\r\n\tat org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMethodInvocationControl(DefaultMockCreator.java:114)\r\n\tat org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:69)\r\n\tat org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46)\r\n\tat org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:73)\r\n<\/code><\/pre>\n<p>These are the GitHub issues that have been opened for this exception &#8211; issue1 and issue2. We can proceed by creating a basic class with a static method.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.mockito.staticmethod;\r\n\r\npublic class Utils {\r\n\r\n\tpublic static boolean print(String msg) {\r\n\t\tSystem.out.println(\"Printing \"+msg);\r\n\t\treturn true;\r\n\t}\r\n}\r\n<\/code><\/pre>\n<h2>Example of JUnit Mockito PowerMock<\/h2>\n<p>To incorporate PowerMock into Mockito and JUnit 4, we must follow these steps.<\/p>\n<ul class=\"post-ul\">\n<li>Annotate test class with @RunWith(PowerMockRunner.class) annotation.<\/li>\n<li>Annotate test class with @PrepareForTest and provide classed to be mocked using PowerMock.<\/li>\n<li>Use PowerMockito.mockStatic() for mocking class with static methods.<\/li>\n<li>Use PowerMockito.verifyStatic() for verifying mocked methods using Mockito.<\/li>\n<\/ul>\n<p>I will provide you with one option for paraphrasing the given statement natively:<\/p>\n<p>&#8220;This JUnit test case demonstrates a comprehensive instance of mocking a static method using Mockito and PowerMock.&#8221;<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.mockito.staticmethod;\r\n\r\nimport static org.junit.Assert.assertFalse;\r\nimport static org.junit.Assert.assertTrue;\r\nimport static org.mockito.ArgumentMatchers.anyString;\r\nimport static org.mockito.Mockito.atLeast;\r\nimport static org.mockito.Mockito.when;\r\n\r\nimport org.junit.Test;\r\nimport org.junit.runner.RunWith;\r\nimport org.powermock.api.mockito.PowerMockito;\r\nimport org.powermock.core.classloader.annotations.PrepareForTest;\r\nimport org.powermock.modules.junit4.PowerMockRunner;\r\n\r\n@RunWith(PowerMockRunner.class)\r\n@PrepareForTest(Utils.class)\r\npublic class JUnit4PowerMockitoStaticTest{\r\n\r\n\t@Test\r\n\tpublic void test_static_mock_methods() {\r\n\t\tPowerMockito.mockStatic(Utils.class);\r\n\t\twhen(Utils.print(\"Hello\")).thenReturn(true);\r\n\t\twhen(Utils.print(\"Wrong Message\")).thenReturn(false);\r\n\t\t\r\n\t\tassertTrue(Utils.print(\"Hello\"));\r\n\t\tassertFalse(Utils.print(\"Wrong Message\"));\r\n\t\t\r\n\t\tPowerMockito.verifyStatic(Utils.class, atLeast(2));\r\n\t\tUtils.print(anyString());\r\n\t}\r\n}\r\n<\/code><\/pre>\n<h2>Example of using TestNG, Mockito, and PowerMock.<\/h2>\n<p>@RunWith annotation is not necessary for TestNG test cases. Instead, we should extend PowerMockTestCase in our test classes in order to create test class instances using PowerMockObjectFactory.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.mockito.staticmethod;\r\n\r\nimport static org.testng.Assert.assertFalse;\r\nimport static org.testng.Assert.assertTrue;\r\n\r\nimport static org.mockito.Mockito.*;\r\nimport org.powermock.api.mockito.PowerMockito;\r\nimport org.powermock.core.classloader.annotations.PrepareForTest;\r\nimport org.powermock.modules.testng.PowerMockTestCase;\r\nimport org.testng.annotations.Test;\r\n\r\n@PrepareForTest(Utils.class)\r\npublic class TestNGPowerMockitoStaticTest extends PowerMockTestCase{\r\n\r\n\t@Test\r\n\tpublic void test_static_mock_methods() {\r\n\t\tPowerMockito.mockStatic(Utils.class);\r\n\t\twhen(Utils.print(\"Hello\")).thenReturn(true);\r\n\t\twhen(Utils.print(\"Wrong Message\")).thenReturn(false);\r\n\t\t\r\n\t\tassertTrue(Utils.print(\"Hello\"));\r\n\t\tassertFalse(Utils.print(\"Wrong Message\"));\r\n\t\t\r\n\t\tPowerMockito.verifyStatic(Utils.class);\r\n\t\tUtils.print(\"Hello\");\r\n\t\tPowerMockito.verifyStatic(Utils.class, times(2));\r\n\t\tUtils.print(anyString());\r\n\t}\r\n}\r\n<\/code><\/pre>\n<h2>Summarize the given information using your own words, providing only one option.<\/h2>\n<p>PowerMock offers additional capabilities for testing static methods in Mockito. It seamlessly works with JUnit 4 and TestNG, but there are currently no plans to provide support for JUnit 5 in the near future.<\/p>\n<p>You have the option to download the entire project from our GitHub Repository.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mockito enables the creation of mock objects. As static methods are associated with the class, Mockito does not offer the capability to mock them. Nevertheless, it is possible to utilize the PowerMock framework in conjunction with Mockito to mock static methods. Use PowerMock to create a Mockito mock for a static method. PowerMock offers various [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[87,132,135,137,131,130,133,138,136,134],"class_list":["post-991","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java","tag-java-testing","tag-junit-4","tag-mock-framework","tag-mockito","tag-powermock","tag-static-methods","tag-test-automation","tag-testng","tag-unit-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PowerMock and Mockito are used to mock static methods in Java - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to use PowerMock with Mockito to mock static methods in Java. Complete guide with JUnit 4 and TestNG examples, dependencies, and best practices.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerMock and Mockito are used to mock static methods in Java\" \/>\n<meta property=\"og:description\" content=\"Learn how to use PowerMock with Mockito to mock static methods in Java. Complete guide with JUnit 4 and TestNG examples, dependencies, and best practices.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-17T11:51:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-18T14:42:09+00:00\" \/>\n<meta name=\"author\" content=\"Olivia Parker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Olivia Parker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/\"},\"author\":{\"name\":\"Olivia Parker\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9\"},\"headline\":\"PowerMock and Mockito are used to mock static methods in Java\",\"datePublished\":\"2023-10-17T11:51:24+00:00\",\"dateModified\":\"2025-07-18T14:42:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/\"},\"wordCount\":438,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"Java\",\"Java testing\",\"JUnit 4\",\"mock framework\",\"Mockito\",\"PowerMock\",\"static methods\",\"test automation\",\"TestNG\",\"unit testing\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/\",\"name\":\"PowerMock and Mockito are used to mock static methods in Java - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2023-10-17T11:51:24+00:00\",\"dateModified\":\"2025-07-18T14:42:09+00:00\",\"description\":\"Learn how to use PowerMock with Mockito to mock static methods in Java. Complete guide with JUnit 4 and TestNG examples, dependencies, and best practices.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerMock and Mockito are used to mock static methods in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9\",\"name\":\"Olivia Parker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g\",\"caption\":\"Olivia Parker\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/oliviaparker\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"PowerMock and Mockito are used to mock static methods in Java - Blog - Silicon Cloud","description":"Learn how to use PowerMock with Mockito to mock static methods in Java. Complete guide with JUnit 4 and TestNG examples, dependencies, and best practices.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/","og_locale":"en_US","og_type":"article","og_title":"PowerMock and Mockito are used to mock static methods in Java","og_description":"Learn how to use PowerMock with Mockito to mock static methods in Java. Complete guide with JUnit 4 and TestNG examples, dependencies, and best practices.","og_url":"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2023-10-17T11:51:24+00:00","article_modified_time":"2025-07-18T14:42:09+00:00","author":"Olivia Parker","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Olivia Parker","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/"},"author":{"name":"Olivia Parker","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9"},"headline":"PowerMock and Mockito are used to mock static methods in Java","datePublished":"2023-10-17T11:51:24+00:00","dateModified":"2025-07-18T14:42:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/"},"wordCount":438,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["Java","Java testing","JUnit 4","mock framework","Mockito","PowerMock","static methods","test automation","TestNG","unit testing"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/","url":"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/","name":"PowerMock and Mockito are used to mock static methods in Java - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2023-10-17T11:51:24+00:00","dateModified":"2025-07-18T14:42:09+00:00","description":"Learn how to use PowerMock with Mockito to mock static methods in Java. Complete guide with JUnit 4 and TestNG examples, dependencies, and best practices.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/powermock-and-mockito-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PowerMock and Mockito are used to mock static methods in Java"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9","name":"Olivia Parker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g","caption":"Olivia Parker"},"url":"https:\/\/www.silicloud.com\/blog\/author\/oliviaparker\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=991"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/991\/revisions"}],"predecessor-version":[{"id":1638,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/991\/revisions\/1638"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}