繁体   English   中英

模拟静态方法链

[英]Mocking a static method chain

我正在尝试模拟静态方法

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

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

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

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

您需要多次。 您需要模拟更多的对象staticMethodcontentType

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

暂无
暂无

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

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