Spring Boot:模拟 SOAP Web 服务

Jas*_*ing 5 java spring integration-testing soap spring-boot

我想知道在 Spring Boot 中模拟 SOAP Web 服务以运行集成测试的最佳实践是什么。我在 spring 网站上能找到的就是https://spring.io/guides/gs/consuming-web-service/。我们是否必须为像模拟依赖项这样简单的事情创建一个 schema/wsdl?

要模拟 REST 服务,我们所要做的就是将 @RestController 注释添加到我们的模拟服务中以使其启动。我一直在寻找一种轻量级的解决方案。

注意:我目前正在使用 REST Assured 进行集成测试。

谢谢!

Dhe*_*rik 1

最简单的方法是模拟负责与 Soap Web 服务集成的 bean。

例如,如果您SoapWebService使用 Soap 与另一个 Web 服务进行此通信,则可以@MockBean在测试中使用注释并模拟返回。例子:

@SpringBootTest
@WebAppConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class YourControllerIT {

    @MockBean
    private SoapWebService soapWebService ;

    @Before
    public void setup() {
        when(soapWebService.soapCall(
                any(), anyLong())).thenReturn("mockedInformation");
    }

    @Test
    public void addPerson() {
         MvcResult mvcResult = mockMvc.perform(post("/api/persons")
                .accept("application/json")
                .header("Content-Type", "application/json")
                .content(jsonContent))
                .andExpect(status().isCreated())
                .andReturn();
    }
}
Run Code Online (Sandbox Code Playgroud)