【问题标题】:PowerMock private method rely on private methodPowerMock 私有方法依赖于私有方法
【发布时间】:2017-05-12 23:58:22
【问题描述】:

我想使用 Powermock 来测试一个私有方法(一个),但是这个私有方法依赖于另一个私有方法(两个)。

所以我必须模拟另一个私有方法。但是当我调试它时,结果发现私有方法一并没有调用模拟私有方法(二),如果我运行而不是调试它会抛出异常:

预计 1 个匹配者,记录 2 个。

private int getCurrentLocaleID(WebIServerSession currentSession, String preferenceName) {
    String prefLocaleID = getUserPreference(currentSession, preferenceName);
    int lcid;

    if (HTTPHelper.isDefaultLocale(prefLocaleID)) {
        prefLocaleID = _appContext.getBrowserHeaderLocaleId();
    }
    try {
        lcid = Integer.parseInt(prefLocaleID);
    } catch (NumberFormatException nfe) {
        lcid = DEFAULT_LCID; // default behavior from old session manager
    }
    return lcid;
}

@Test
public void getCurrentLocaleID() throws Exception {
    PowerMockito.mockStatic(HTTPHelper.class);
    WebAppSessionManagerImpl webAppSessionMangerImpl2 = PowerMockito.spy(new WebAppSessionManagerImpl(appConext));
    given(HTTPHelper.isDefaultLocale("1")).willReturn(true);
    given(HTTPHelper.isDefaultLocale("0")).willReturn(false);
    given(appConext.getBrowserHeaderLocaleId()).willReturn("1");
    PowerMockito.doReturn("1").when(webAppSessionMangerImpl2, "getUserPreference", anyObject(), anyString());
    int result = Whitebox.invokeMethod(webAppSessionMangerImpl2, "getCurrentLocaleID", webIserverSession, "test");
    assertEquals(result, 1);
}

【问题讨论】:

  • 仔细检查:你的测试类必须用@RunWith(PowerMockRunner.class) @PrepareForTest({ HTTPHelper.class, WebAppSessionManagerImpl.class })注解

标签: java unit-testing mockito powermockito


【解决方案1】:

不要测试私有方法。如果必须这样做,这意味着您的班级做的太多了,而且不符合单一职责原则。

这是在专门的类中进行一些重构和隔离逻辑的机会,如下所示:

public class SpecializedClass{

    private Context context;

    public SpecializedClass(Context context){
         this.context = context;
    }

        public int getCurrentLocaleID(WebIServerSession currentSession, String preferenceName) {
        String prefLocaleID = getUserPreference(currentSession, preferenceName);
        int lcid;

        if (HTTPHelper.isDefaultLocale(prefLocaleID)) {
            prefLocaleID = _appContext.getBrowserHeaderLocaleId();
        }

        try {
            lcid = Integer.parseInt(prefLocaleID);
        } catch (NumberFormatException nfe) {
            lcid = DEFAULT_LCID; // default behavior from old session manager
        }
        return lcid;
    }

    String getUserPreference(Session session, String preferenceName){..}
}

现在将方法 public 和 getUserPreference 标记为包级别,测试看起来像:

@Test
public void getCurrentLocaleID() throws Exception {
    PowerMockito.mockStatic(HTTPHelper.class);

    SpecializedClass specializedClassSpy = Mockito.spy(new SpecializedClass(appConext));

    given(HTTPHelper.isDefaultLocale("1")).willReturn(true);
    given(HTTPHelper.isDefaultLocale("0")).willReturn(false);
    given(appConext.getBrowserHeaderLocaleId()).willReturn("1");

    Mockito.doReturn("1").when(specializedClassSpy)
       .getUserPreference(webIserverSession, "test");

    int result = specializedClassSpy.getCurrentLocaleID(webIserverSession, "test");

    assertEquals(result, 1);
}

【讨论】:

    猜你喜欢
    • 2014-10-15
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    相关资源
    最近更新 更多