如何测试在Spring Boot中调用另一个SOAP Web服务的REST Web服务。

huangapple go评论106阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月29日 23:07:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63648474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定