使用EasyMock的PowerMock Java URL

使用EasyMock的PowerMock Java URL,java,easymock,powermock,Java,Easymock,Powermock,我有以下代码 @Test public void wrapSoapTest1() throws TransformerConfigurationException, IOException { RequestUtil r = new RequestUtil(SAMPLE_REQUEST_BEFORE.getBytes(),""); URL url = PowerMock.createNiceMock(URL.class); r.setXslUrl(url); Ea

我有以下代码

@Test
public void wrapSoapTest1() throws TransformerConfigurationException, IOException {
    RequestUtil r = new RequestUtil(SAMPLE_REQUEST_BEFORE.getBytes(),"");
    URL url = PowerMock.createNiceMock(URL.class);
    r.setXslUrl(url);
    EasyMock.expect(url.openStream()).andReturn(IOUtils.toInputStream(XSLT, "UTF-8"));
    Assert.assertEquals(SAMPLE_REQUEST_AFTER, new String(r.wrapSOAP()));
}
当我运行这个程序时,虽然我得到了以下错误

java.lang.AbstractMethodError: java/net/URLStreamHandler.openConnection(Ljava/net/URL;)Ljava/net/URLConnection;
at java.net.URL.openConnection(URL.java:957)

有没有办法模拟URL并发送自定义字符串作为响应?

如果使用
PowerMock
您必须确保您的测试用例用
@RunWith(PowerMockRunner.class)
注释。然后必须告诉
PowerMock
为测试准备
URL
类(
@PrepareForTest({URL.class})
),并且应该使用
PowerMock.expectLastCall()
而不是
EasyMock
方法。至少您必须将模拟设置为重播模式

这应该行得通

@RunWith(PowerMockRunner.class)
@PrepareForTest({ URL.class })
public class SoapTest {
    @Test
    public void wrapSoapTest1() throws TransformerConfigurationException,
        IOException {
        RequestUtil r = new RequestUtil(SAMPLE_REQUEST_BEFORE.getBytes(),"");
        URL url = PowerMock.createNiceMock(URL.class);
        r.setXslUrl(url);
        url.openStream();
        PowerMock.expectLastCall().andReturn(IOUtils.toInputStream(XSLT, "UTF-8"));
        PowerMock.replay(url);
        Assert.assertEquals(SAMPLE_REQUEST_AFTER, new String(r.wrapSOAP()));
    }
}

我的版本看起来像这样

private URL mockURL() throws IOException{
    final URLConnection mockConnection = PowerMock.createMock(URLConnection.class);
    InputStream i = IOUtils.toInputStream(XSLT, "UTF-8");
    EasyMock.expect(mockConnection.getInputStream()).andReturn(i);
    EasyMock.replay(mockConnection);
    final URLStreamHandler handler = new URLStreamHandler() {
        @Override
        protected URLConnection openConnection(final URL arg0)
                throws IOException {
            return mockConnection;
        }
    };
    return new URL("http://foo.bar", "foo.bar", 80, "", handler);
}

我有TestNG、PowerMock和EasyMock,唯一的区别是我必须添加@PrepareForTest({MyTestedClass.class}),在我的例子中,MyTestedClass是UrlScreenRequest

    @PrepareForTest({UrlScreenRequest.class})
    public class UrlScreenRequestTest  extends PowerMockTestCase {
    ...
    @Test
    public void testValidatePermission() throws Exception {
      URL url=PowerMock.createNiceMock(URL.class);
      HttpURLConnection connection = createMock(HttpURLConnection.class);
      url.openConnection();
      PowerMock.expectLastCall().andReturn(connection);
      PowerMock.replayAll(url, connection);
      UrlScreenRequest request = new UrlScreenRequest(url);
      request.validatePermission();
      //validation
    }
    ...

试过了,但还是没用,不得不模仿URLConnection(?)我会在稍后发布更多信息。这对我们也有用,如果你不想更改处理程序,请查看我的解决方案。