【问题标题】:Mocking a static method chain模拟静态方法链
【发布时间】:2018-08-21 23:26:18
【问题描述】:

我正在尝试模拟一个静态方法

StaticClass.staticMethod(id).setContentType("text/plain").build();

我能够使用 PowerMockito 模拟静态方法及其返回类型,如下所示:

PowerMockito.when(StaticClass.staticMethod(id)).thenReturn(returnValue);

但是如何将该值发送到链式方法 setContentType()?

【问题讨论】:

    标签: java junit static powermock powermockito


    【解决方案1】:

    您需要多个时间。您需要模拟更多对象staticMethodcontentType

    PowerMockito.when(StaticClass.staticMethod(id)).thenReturn(staticMethod);
    PowerMockito.when(staticMethod.setContentType("text/plain")).thenReturn(contentType);
    PowerMockito.when(contentType.build()).thenReturn(returnValue);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多