我将MockWebServiceServer用于单元测试客户端,该客户端扩展了WebServiceGatewaySupport并调用getWebServiceTemplate()。marshalSendAndReceive()。即使设置了模拟服务器,它也会调用实际的api,我会丢失任何东西吗?根据Spring网站上的文档,我已经正确配置了模拟服务器以绕过实际的api调用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class ValidateServiceTest {
@Configuration
static class ContextConfiguration {
@Bean
public Jaxb2Marshaller marshallerATP() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("xyz.wsdl");
return marshaller;
}
@Bean
public ValidateClient validateClient(Jaxb2Marshaller marshallerATP) {
ValidateClient client = new ValidateClient();
client.setDefaultUri("test");
client.setMarshaller(marshallerATP);
client.setUnmarshaller(marshallerATP);
return client;
}
}
@Autowired
private ValidateClient validateClient;
@Autowired
ApplicationContext applicationContext;
**private MockWebServiceServer mockServer;**
@Before
public void setUp() throws Exception {
...
**mockServer = MockWebServiceServer.createServer(applicationContext);**
}
@Test
public void test() throws Exception {
Source expectedRequestPayload = new StringSource(
readFile("....json",
StandardCharsets.US_ASCII));
Source responsePayload = new StringSource(
readFile("....json",
StandardCharsets.US_ASCII));
mockServer.expect(RequestMatchers.payload(expectedRequestPayload))
.andRespond(ResponseCreators.withPayload(responsePayload));
requestObj = mapper.readValue(
ResourceUtils.getFile(
"....json"),
ValidateRequest.class);
ValidateResponse expected1 = toaValidateClient.validate(requestObj, userContext);
**mockServer.verify();**
}