英文:
how to test a REST web service that calls another SOAP web service in Spring Boot
问题
我目前正在使用Spring Boot。我遇到了一个问题:我的REST服务正在调用SOAP服务,我想单元测试我的服务,以确保来自SOAP服务的XML响应与REST服务的JSON响应匹配。
我该如何实现这一目标?
英文:
I am currently working on Spring Boot. I am stuck with a problem: My REST service is calling SOAP service and I want to unit test my service whether the XML response from SOAP service matches the REST service response in JSON.
How can I achieve this?
答案1
得分: 3
通常情况下,您可以使用模拟来完成这样的操作。在测试过程中,您会注入一个行为与目标SOAP服务完全相同的类,但它被硬编码为返回一些预定值。有关示例,请参阅Mockito教程。简言之,您要求Spring将一个模拟的bean注入到您的服务中,而不是真实的bean。
您还可以模拟实际的服务,例如使用类似Wiremock的工具。在这种情况下,您会在Spring配置中更改URL,使其指向Wiremock而不是实际服务。
英文:
Usually you do such things with mocking. During the test, you inject a class that behaves exactly like the target SOAP service, but is hardcoded to return some predetermined value. See Mockito tutorial for example. In a nutshell, you tell Spring to inject a mocked bean into your service instead of the real one.
You can also mock the actual service, for example with something like Wiremock. In this case, you change the URL in Spring configuration to point to Wiremock instead of the actual service.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论