How to Mock Static method used in another static method

37 views
Skip to first unread message

Anesti Andrioti

unread,
Jan 17, 2021, 3:01:20 PM1/17/21
to mockito
Hello,

I've read that I can mock static methods since 3.4.0
I am trying to mock a static method that is used in another static method, but I am having issues with this.

What I am looking for, is something like the bellow

public class MyStaticClass {

    public static String speak() {
        return sayHello();
    }

    public static String saySpeech() {
        return "Speech"
    }
}


@Test
void myTest() {

    try (MockedStatic<MyStaticClass> MyStaticClassMock = Mockito.mockStatic(MyStaticClass.class)) {
        MyStaticClassMock.when(MyStaticClass::saySpeach).thenReturn("MockedSpeech");

        assertEquals("MockedSpeech", MyStaticClass.speak());
    }

    assertEquals("Speech", MyStaticClass.speak());
}

I would appreciate any help!

Tim van der Lippe

unread,
Jan 17, 2021, 3:03:32 PM1/17/21
to mockito
If you want to invoke the real method when invoking `MyStaticClass.speak()`, you can add the following stub:

MyStaticClassMock.when(MyStaticClass::speak).thenCallRealMethod();

Op zo 17 jan. 2021 om 20:01 schreef Anesti Andrioti <anestia...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/ed2c783e-563b-4061-b0a3-77a2fc99b63bn%40googlegroups.com.

Anesti Andrioti

unread,
Jan 17, 2021, 3:34:43 PM1/17/21
to mockito
Hey!

Sorry, I think I was not clear in my question.

I don't want to call the real method. I actually want the opposite.
What I am trying to do is have speak() use the mocked saySpeech() method.  

I don't really understand why that's not working. 
My goal is to make myTest() work.

Any idea of how I can mock a static method, and use the mocked result in another static method?

Thank you !
PS: I corrected in red a small mistake in my code example 

On Sunday, January 17, 2021 at 10:03:32 PM UTC+2 tvande...@gmail.com wrote:
If you want to invoke the real method when invoking `MyStaticClass.speak()`, you can add the following stub:

MyStaticClassMock.when(MyStaticClass::speak).thenCallRealMethod();

Op zo 17 jan. 2021 om 20:01 schreef Anesti Andrioti <anestia...@gmail.com>:
Hello,

I've read that I can mock static methods since 3.4.0
I am trying to mock a static method that is used in another static method, but I am having issues with this.

What I am looking for, is something like the bellow

public class MyStaticClass {

    public static String speak() {
        return  saySpeech();
    }

    public static String saySpeech() {
        return "Speech"
    }
}


@Test
void myTest() {

    try (MockedStatic<MyStaticClass> MyStaticClassMock = Mockito.mockStatic(MyStaticClass.class)) {
        MyStaticClassMock.when(MyStaticClass::saySpeach).thenReturn("MockedSpeech");

        assertEquals("MockedSpeech", MyStaticClass.speak());
    }

    assertEquals("Speech", MyStaticClass.speak());
}

I would appreciate any help!

Tim van der Lippe

unread,
Jan 17, 2021, 3:35:18 PM1/17/21
to mockito
Hm, that sounds like a bug to me. Please open an issue on GitHub and we can investigate further.

Op zo 17 jan. 2021 om 20:34 schreef Anesti Andrioti <anestia...@gmail.com>:

Anesti Andrioti

unread,
Jan 17, 2021, 4:15:06 PM1/17/21
to mockito
Done! https://github.com/mockito/mockito/issues/2182

Thank you for the quick replies! :)

Reply all
Reply to author
Forward
0 new messages