我有以下代码...
@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()));
}
}
关于java - 使用 EasyMock 的 PowerMock Java URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21739760/