Mockito.mockedStatic for method with arguments

后端 未结 1 721
花落未央
花落未央 2021-01-03 06:58

All the examples provided for mockedStatic method is for method without parameters. Is there a way to mock methods with parameters.

examples provided: https://javadoc

相关标签:
1条回答
  • 2021-01-03 07:29

    It is possible, you need to use a lambda instead of a method reference:

    try (MockedStatic<Foo> dummyStatic = Mockito.mockStatic(Foo.class)) {
        dummyStatic.when(() -> Foo.method("param1"))
                   .thenReturn("someValue");
        // when
        System.out.println(Foo.method("param1"));
        //then
        dummyStatic.verify(
                times(1), 
                () -> Foo.method("param1")
        );
    }
    
    0 讨论(0)
提交回复
热议问题