8

I'm using Mockito, along with mockito-inline for mocking static methods. I'm trying to apply doNothing or similar behavior, to a static void method. The following workaround work, but I think that there should have a more convenient way to achieve this with less code.

try (MockedStatic<UtilCalss> mock = Mockito.mockStatic(UtilCalss.class)) {

     mock.when(() -> UtilCalss.staticMethod(any()))
            .thenAnswer((Answer<Void>) invocation -> null);

}

If it's a non-static method, we could simply do:

doNothing().when(mock).nonStaticMethod(any());

But I want to do the same for a static method.

Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68

2 Answers2

18

You don't need to stub that call.

doNothing is a default behaviour of a void method called on a mock.

Example:

Class under test:

public class UtilClass {
    public static void staticMethod(String data) {
        System.out.println("staticMethod called: " + data);
    }
}

Test code:

public class UtilClassTest {
    @Test
    void testMockStaticForVoidStaticMethods() {
        try (MockedStatic<UtilClass> mockStatic = Mockito.mockStatic(UtilClass.class)) {
            UtilClass.staticMethod("inMockStaticScope");
        }
        UtilClass.staticMethod("outOfMockStaticScope");
    }
}

Output:

staticMethod called: outOfMockStaticScope
Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • 3
    Could you provide an example of how to get it to work? At least the above example in the question doesn't work. – OuuGiii Jan 14 '22 at 15:50
  • And what about for non-void methods? – Matt Apr 05 '23 at 00:08
  • @Matt same as for non-void non-static methods. If not stubbed, they return default value for their return type. – Lesiak Apr 05 '23 at 06:03
  • 1
    If I leave it as objMock.when(() -> Obj.staticFunc(any())) I get an error during execution "UnfinishedStubbingException". I don't see why this answer has the highest score. – Tim T Jun 16 '23 at 03:42
  • @OuuGiii just declare the mock : MockedStatic mock = Mockito.mockStatic(UtilCalss.class) then call the method under test classUnderTest.methodUsingTheMock() – Philippe Simo Jun 20 '23 at 14:06
  • Some examples please! – user1034912 Aug 01 '23 at 09:23
5

mockito-inline include mockito-core :

        <!-- Mockito-inline include Mockito-code in same version - Useful for Mock static method -->
        <!-- See https://asolntsev.github.io/en/2020/07/11/mockito-static-methods/ -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>3.6.28</version>
        </dependency>

And if static method you are testing return nothing => don't use mock.when => just execute the method you want to test and verify :

try (MockedStatic<UtilClass> mock = Mockito.mockStatic(UtilClass.class)) {

     mock.when(() -> UtilClass.staticMethod(any()))
            .thenAnswer((Answer<Void>) invocation -> null);

     App.main(null); // example : I'm testing main method from App class

     mock.verify(UtilClass::staticMethod); // I verify static method from UtilClass was called.

}

resources :

Vifier Lockla
  • 512
  • 5
  • 6