<分区>
我想对我的整个应用程序编写一个集成测试,并且只想模拟一个特定的方法:我用来将一些数据发送到外部网络服务并接收响应的 RestTemplate
.
我想从本地文件读取响应(模拟和模仿外部服务器响应,所以它总是一样的)。
我的本地文件应该只包含在生产环境中外部网络服务器会响应的json/xml
响应。
问题:如何模拟外部 xml 响应?
@Service
public class MyBusinessClient {
@Autowired
private RestTemplate template;
public ResponseEntity<ProductsResponse> send(Req req) {
//sends request to external webservice api
return template.postForEntity(host, req, ProductsResponse.class);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test {
@Test
public void test() {
String xml = loadFromFile("productsResponse.xml");
//TODO how can I tell RestTemplate to assume that the external webserver responded with the value in xml variable?
}
}