我如何模拟SOAPConnection和SOAPConnectionFactory

时间:2016-10-19 15:27:58

标签: java soap junit easymock

这是我的代码:

URL url = new URL(getURL());  
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnFactory.createConnection();`
response=soapConnection.call(soapMsg, url);

我试图为SOAPConnection创建一个模拟但我得到一个例外。 任何人都可以建议我解决这个问题吗?

我的测试是这样的: 我创建了一个SOAPMessage对象和URL对象。

Resource classUnderTest = new Resource(); 
SOAPConnection mockConnection = EasyMock.createMock(SOAPConnection.class); 
EasyMock.expect(mockConnection.call(soapmsg, url)).andReturn(soapMsg); 
EasyMock.replay(mockConnection); 
WhiteBox.setinternalState(classUnderTest, SOAPConnection.class, mockConnection);

1 个答案:

答案 0 :(得分:0)

更好的方法可能是在localhost端口上模拟整个服务。我写了一篇small library来完成大部分工作。