Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7609701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:14:12+00:00 2026-05-31T01:14:12+00:00

I have a few static util methods in my project, some of them just

  • 0

I have a few static util methods in my project, some of them just pass or throw an exception. There are a lot of examples out there on how to mock a static method that has a return type other than void. But how can I mock a static method that returns void to just "doNothing()"?

The non-void version uses these lines of code:

@PrepareForTest(StaticResource.class)

…

PowerMockito.mockStatic(StaticResource.class);

…

Mockito.when(StaticResource.getResource("string")).thenReturn("string");

However if applied to a StaticResources that returns void, the compile will complain that when(T) is not applicable for void…

Any ideas?

A workaround would probably be to just have all static methods return some Boolean for success but I dislike workarounds.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:14:13+00:00Added an answer on May 31, 2026 at 1:14 am

    Since Mockito 3.4.0, an experimental API was introduced to mock static methods.

    The following example code has been tested with Mockito 4.3.1 (testImplementation("org.mockito:mockito-inline:4.3.1), and JUnit Jupiter 5.8.2, OpenJDK 11.

    import org.junit.jupiter.api.Assertions;
    import org.junit.jupiter.api.Test;
    import org.mockito.MockedStatic;
    import org.mockito.Mockito;
    
    import java.util.UUID;
    
    public class StaticMockTest {
    
        @Test
        void showCaseStaticMock() {
            try (MockedStatic<StaticMockTest> staticMock = Mockito.mockStatic(StaticMockTest.class)) {
                staticMock.when(StaticMockTest::getUUIDValue).thenReturn("Mockito");
                Assertions.assertEquals("Mockito", StaticMockTest.getUUIDValue());
            }
    
            // Regular UUID
            UUID.fromString(StaticMockTest.getUUIDValue());
        }
    
        public static String getUUIDValue() {
            return UUID.randomUUID().toString();
        }
    }
    

    Previous Answer, probably Mockito 1.x/2.x with Powermock 1.x/2.x

    You can do it the same way you do it with Mockito on real instances. For example you can chain stubs, the following line will make the first call do nothing, then second and future call to getResources will throw the exception :

    // the stub of the static method
    doNothing().doThrow(Exception.class).when(StaticResource.class);
    StaticResource.getResource("string");
        
    // the use of the mocked static code
    StaticResource.getResource("string"); // do nothing
    StaticResource.getResource("string"); // throw Exception
    

    Thanks to a remark of Matt Lachman, note that if the default answer is not changed at mock creation time, the mock will do nothing by default. Hence writing the following code is equivalent to not writing it.

    doNothing().doThrow(Exception.class).when(StaticResource.class);
        StaticResource.getResource("string");
    

    Though that being said, it can be interesting for colleagues that will read the test that you expect nothing for this particular code. Of course this can be adapted depending on how is perceived understandability of the test.


    By the way, in my humble opinion you should avoid mocking static code if your crafting new code. At Mockito we think it’s usually a hint to bad design, it might lead to poorly maintainable code. Though existing legacy code is yet another story.

    Generally speaking if you need to mock private or static method, then this method does too much and should be externalized in an object that will be injected in the tested object.

    Hope that helps.

    Regards

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few static household ads in an UIImageView that just display some
In C# 3.5 in a class I have a few static methods with some
I have a few static pages that are just pure HTML, that we display
I have some TableViewController with static Cells and some TextFields. A few Days ago,
I have a simple solution in Visual Studio 2010 containing a few static libraries,
I have written a mini web-server which needs to serve up a few static
I have upgraded my grails app from 1.3.7 to 2.0.1. I had few static
I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{
I have tried a few different methods, like print(boolean isLeapYear) and a few others,
I have a utility method (= a static one) that I call a lot,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.