简体   繁体   English

PowerMock:静态方法的模拟(+ 在某些特定方法中返回原始值)

[英]PowerMock: mocking of static methods (+ return original values in some particular methods)

I use PowerMock 1.4.7 and JUnit 4.8.2我使用 PowerMock 1.4.7 和 JUnit 4.8.2

I need to mock only some static methods and I want others (from the same class) just to return original value.我只需要模拟一些静态方法,而我希望其他方法(来自同一类)只返回原始值。 When I mock with mockStatic and don't call when().doReturn() all static methods return their defaults - like null when returning Object or false when returning boolean...etc.当我用mockStatic模拟并且不调用when().doReturn()所有静态方法都返回它们的默认值 - 例如返回 Object 时为 null 或返回布尔值时为 false 等。 So I try to use thenCallRealMethod explicitly on each static method to return default implementation (means no mocking/ no fakes) but I don't know how to call it on every possible arguments variations (= I want for every possible input call original method).所以我尝试在每个静态方法上显式使用thenCallRealMethod来返回默认实现(意味着没有模拟/没有假货)但我不知道如何在每个可能的参数变化上调用它(=我想要每个可能的输入调用原始方法) . I only know how to mock concrete argument variation.我只知道如何模拟具体的参数变化。

You can use a spy on your static class and mock only specific methods:您可以对静态类使用间谍并仅模拟特定方法:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyStaticTest.MyStaticClass.class)
public class MyStaticTest {

public static class MyStaticClass {
    public static String getA(String a) {
        return a;
    }
    public static String getB(String b) {
        return b;
    }
}

@Test
public void should_partial_mock_static_class() throws Exception {
    //given
    PowerMockito.spy(MyStaticClass.class);
    given(MyStaticClass.getB(Mockito.anyString())).willReturn("B");
    //then
    assertEquals("A", MyStaticClass.getA("A"));
    assertEquals("B", MyStaticClass.getA("B"));
    assertEquals("C", MyStaticClass.getA("C"));
    assertEquals("B", MyStaticClass.getB("A"));
    assertEquals("B", MyStaticClass.getB("B"));
    assertEquals("B", MyStaticClass.getB("C"));
}

}

You can also use the stubbing API:您还可以使用存根 API:

stub(method(MyStaticClass.class, "getB")).toReturn("B");

Edit:编辑:

To use stub and method statically import methods from these packages:要使用stubmethod从这些包静态导入方法:

  1. org.powermock.api.support.membermodification.MemberModifier
  2. org.powermock.api.support.membermodification.MemberMatcher

For more info refer to the documentation有关更多信息,请参阅文档

I managed to use spy and doReturn to achieve it.我设法使用spydoReturn来实现它。

class MyStatic {
    static String foo() { return "foo"; }
    static String foobar() { return foo() + "bar"; }
}

@Test
public void thisShouldSpyStaticMethods() {
    // arrange
    spy(MyStatic.class);
    doReturn("mocked foo").when(MyStatic.class);
    MyStatic.foo();

    // act
    final String result = MyStatic.foobar();

    // assert
    assertThat(result).isEqualTo("mocked foobar");
}

The doReturn followed by a call to the method to be mocked looks weird (at least to me), but seems to do the trick. doReturn然后调用要doReturn的方法看起来很奇怪(至少对我而言),但似乎可以解决问题。

Using spy with when(MyStatic.foo()).thenReturn("mocked foo") doesn't work for me.spywhen(MyStatic.foo()).thenReturn("mocked foo")使用对我不起作用。

PowerMockito's documentation on mocking static method . PowerMockito 关于模拟静态方法的文档。

Based on this question PowerMockito mock single static method and return object基于这个问题PowerMockito 模拟单个静态方法和返回对象

PowerMockito.mockStatic(MyStaticClass.class);

alone does not mock all methods (in recent versions of PowerMockito at least), only enables mocking later of individual methods.单独不会模拟所有方法(至少在 PowerMockito 的最新版本中),只能在以后模拟单个方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM