{"id":1541,"date":"2022-04-12T09:16:56","date_gmt":"2022-04-11T21:16:56","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1541"},"modified":"2022-04-12T09:16:56","modified_gmt":"2022-04-11T21:16:56","slug":"mocking-static-method-with-mockito","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1541","title":{"rendered":"Mocking Static Method with Mockito"},"content":{"rendered":"<p>Sample codes for mocking a static with Mockito.<\/p>\n<p>The StringUtil class is a very trivial class that converts the text to all capitalized. <\/p>\n<h2>StringUtil class<\/h2>\n<pre><code class=\"language-java\">public final class StringUtil {\n\n    private StringUtil() {}\n\n    public static String upperCase(String text) {\n        return text.toUpperCase();\n    }\n}<\/code><\/pre>\n<h2>Mocking the StringUtil.upperCase method<\/h2>\n<p>Stubbing the <strong>StringUtil.upperCase method<\/strong> to <strong>return test<\/strong> if <strong>test is passed as argument<\/strong>.<\/p>\n<h3>Dependencies Required<\/h3>\n<pre><code>org.junit.jupiter:junit-jupiter-engine:5.8.2\norg.mockito:mockito-inline:4.4.0<\/code><\/pre>\n<h3>StringUtilTest class<\/h3>\n<pre><code class=\"language-java\">import org.junit.jupiter.api.Test;\nimport static org.mockito.Mockito.*;\nimport static org.junit.jupiter.api.Assertions.*;\n\npublic class StringUtilTest {\n\n    @Test\n    void staticMethod() {\n        try (var utilities = mockStatic(StringUtil.class)) {\n            utilities.when(() -&gt; StringUtil.upperCase(&quot;test&quot;)).thenReturn(&quot;test&quot;);\n            assertEquals(&quot;test&quot;, StringUtil.upperCase(&quot;test&quot;));\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sample codes for mocking a static with Mockito. The StringUtil class is a very trivial class that converts the text to all capitalized. StringUtil class public final class StringUtil { private StringUtil() {} public static String upperCase(String text) { return text.toUpperCase(); } } Mocking the StringUtil.upperCase method Stubbing the StringUtil.upperCase method to return test if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,76],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1541"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1541"}],"version-history":[{"count":1,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1541\/revisions"}],"predecessor-version":[{"id":1542,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1541\/revisions\/1542"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}