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

时间:2015-12-30 16:47:30

标签: testng powermock easymock

给出代码

@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");

堆栈跟踪:

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时做错了。

1 个答案:

答案 0 :(得分:0)

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