java Spring Web 服务调用的 Mockito 模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16081598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito pattern for a Spring web service call
提问by shinynewbike
My class under test has this method
我被测的班级有这个方法
public SomeWebServiceResponse callDownstream(SomeWebServiceRequest request) {
return (SomeWebServiceResponse ) super.callService(request);
}
the super method is just a call to Spring WS to make the call - in simplified form
super 方法只是调用 Spring WS 来进行调用 - 以简化形式
response = getWebServiceTemplate().marshalSendAndReceive(this.getBaseURL(),
request);
return response;
When I write a unit test it tried to make an actual web service call. I'm not clear how to mock this or rather what we should be mocking.
当我编写单元测试时,它尝试进行实际的 Web 服务调用。我不清楚如何嘲笑这个,或者更确切地说,我们应该嘲笑什么。
Should I be loading a sample response from filesystem and looking for some string in it - in that case I'm only testing file loading.
我是否应该从文件系统加载示例响应并在其中查找一些字符串 - 在这种情况下,我只是在测试文件加载。
The actual call is in base class and I know we can't mock only that method. Any pointers?
实际调用是在基类中,我知道我们不能只模拟那个方法。任何指针?
采纳答案by rowing-ghoul
Spring does also provide facilities for mocking web service servers as well as requests from clients. The chapter 6.3 in the Spring WS manualshows how to do mocking.
Spring 还提供了用于模拟 Web 服务服务器以及来自客户端的请求的工具。Spring WS 手册中的第 6.3 章展示了如何进行模拟。
The Spring WS mocking facility changes the behaviour of the Web Service Template, so you can call that method in the super-class - that method would then call the Spring Mock Service Server.
Spring WS 模拟工具更改了 Web 服务模板的行为,因此您可以在超类中调用该方法 - 然后该方法将调用 Spring Mock 服务服务器。
Here is a sample unit test with the Spring mock service server:
这是使用 Spring 模拟服务服务器的示例单元测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-ws.xml"})
public class SetStatusFromSrsTemplateTest {
@Autowired
private WebServiceTemplate wsTemplate;
@Before
public void setUp() throws Exception {
mockServer = MockWebServiceServer.createServer(wsTemplate);
}
@Test
public void testCall() {
SomeWebServiceRequest sampleRequest = new SomeWebServiceRequest();
// add properties to the sampleRequest...
Source expectedPayload = new ResourceSource(new ClassPathResource("exampleRequest.xml"));
Source expectedResponse = new ResourceSource(new ClassPathResource("exampleResponse.xml"));
mockServer.expect(payload(expectedPayload)).andRespond(withPayload(expectedResponse));
instance.callDownStream(sampleRequest);
mockServer.verify();
}
}
The above example will make the mock service server expect exactly one request with the given payload and (if the payload received matches the expected payload) respond with the given response payload.
上面的示例将使模拟服务服务器期望使用给定的有效负载恰好是一个请求,并且(如果收到的有效负载与预期的有效负载匹配)以给定的响应有效负载进行响应。
However, if you only want to verify that the method in the super-class is really called during the test and if you're not interested in the message exchange following that call, you should use Mockito.
但是,如果您只想验证在测试期间是否真的调用了超类中的方法,并且如果您对该调用之后的消息交换不感兴趣,则应该使用 Mockito。
If you'd like to use Mockito, I'd suggest the spy (see also Kamlesh's answer). E.g.
如果您想使用 Mockito,我建议使用 spy(另请参阅 Kamlesh 的回答)。例如
// Decorates this with the spy.
MyClass mySpy = spy(this);
// Change behaviour of callWebservice method to return specific response
doReturn(mockResponse).when(mySpy).callWebservice(any(SomeWebServiceRequest.class));
// invoke the method to be tested.
instance.callDownstream(request);
// verify that callWebService has been called
verify(mySpy, times(1)).callWebService(any(SomeWebServiceRequest.class));