在我的EasyMock中,我有一个来自测试类内对象的错误's方法



给定代码

@Test
public void testXClass(){
    XClass xclass = new XClass();
    UriBuilder uriBuilder = EasyMock.createMock(UriBuilder.class);
    EasyMock.expect(uriBuilder.fromUri(EasyMock.anyObject(String.class))).andReturn(dummyUriBuilder()).anyTimes();
    //EasyMock.replay(uriBuilder);
    EasyMock.expect(xclass.xMethod(EasyMock.anyObject(String.class))).andReturn("Done").anyTimes();
    String x  = xclass.xMethod(EasyMock.anyObject(String.class));
}
private UriBuilder dummyUriBuilder() {
    UriBuilder uriBuilder = UriBuilder.fromUri("https://dev.x-service.com");
    uriBuilder.path("/x1/");
    return uriBuilder;
}
class XClass {
    public String xMethod(String x) {
        ...
        UriBuilder uriBuilder = UriBuilder.fromUri("https://dev.x-service.com");
        uriBuilder.path("/x1/");
        ...
    }
}

线路出错

UriBuilder uriBuilder = UriBuilder.fromUri("https://dev.x-service.com");

Stacktrace:

java.lang.IllegalArgumentException at
  org.apache.cxf.jaxrs.impl.UriBuilderImpl.uri(UriBuilderImpl.java:888) at
  javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)

我该如何嘲笑UriBuilder的东西?我认为我设置EasyMock是错误的。

您需要模拟静态方法UriBuilder.fromUri。EasyMock无法做到这一点。你需要PowerMock。

相关内容

  • 没有找到相关文章

最新更新