Java okhttp的存根响应
我正在写一个测试。我的服务看起来像:Java okhttp的存根响应,java,rest,testing,okhttp,Java,Rest,Testing,Okhttp,我正在写一个测试。我的服务看起来像: @Slf4j @Service public class ServiceImpl implements Service { private final OkHttpClient client; private final ObjectMapper objectMapper; private final SettingsService settingsService; private Map<String, Strin
@Slf4j
@Service
public class ServiceImpl implements Service {
private final OkHttpClient client;
private final ObjectMapper objectMapper;
private final SettingsService settingsService;
private Map<String, String> settingsMap;
public ServiceImpl(OkHttpClient client, ObjectMapper objectMapper, SettingsService settingsService) {
this.client = client;
this.objectMapper = objectMapper;
this.settingsService = settingsService;
}
@PostConstruct
public void init() {
settingsMap = settingsService.getSettingsMap();
}
@Override
public void update() {
Request request = new Request.Builder()
.url("https://" + settingsMap.get("x-host") + "/v2/country/2020")
.get()
.addHeader("x-host", settingsMap.get("x-host"))
.addHeader("x-key", settingsMap.get("x-key"))
.build();
try {
Response response = client.newCall(request).execute();
Dto dto = objectMapper.readValue(response.body().string(), dto.class);
} catch (Exception e) {
log.info("Error " + e.getMessage());
}
}
}
@Slf4j
@服务
公共类ServiceImpl实现服务{
私人最终Okhttp客户;
私有最终ObjectMapper ObjectMapper;
私人最终设置服务设置服务;
私人地图设置SMAP;
公共服务impl(OkHttpClient客户端、对象映射器对象映射器、设置服务设置服务){
this.client=client;
this.objectMapper=objectMapper;
this.settings服务=settings服务;
}
@施工后
公共void init(){
settingsMap=settingsService.getSettingsMap();
}
@凌驾
公共无效更新(){
Request Request=newrequest.Builder()
.url(“https://”+设置映射.get(“x-host”)+“/v2/country/2020”)
.get()
.addHeader(“x-host”,setingsmap.get(“x-host”))
.addHeader(“x键”,设置映射获取(“x键”))
.build();
试一试{
Response=client.newCall(request.execute();
Dto Dto=objectMapper.readValue(response.body().string(),Dto.class);
}捕获(例外e){
log.info(“错误”+e.getMessage());
}
}
}
还有我的测试:
@Testcontainers
@SpringBootTest(classes = MyApplication.class)
public class ServiceTest {
public static MockWebServer mockBackEnd;
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
@Container
private static final PostgreSQLContainer<?> container = new PostgreSQLContainer<>();
@MockBean
private Service service;
@MockBean
private SettingsService settingsService;
@BeforeAll
static void setUp() throws IOException {
mockBackEnd = new MockWebServer();
mockBackEnd.url("https://api-v1.p.com/v2/country/2020");
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch (RecordedRequest request) throws InterruptedException {
switch (request.getPath()) {
case "api-v1.p.com":
return new MockResponse().setResponseCode(200).setBody("ex1");
case "/v2/country/2020":
return new MockResponse().setResponseCode(200).setBody("ex2");
case "/":
return new MockResponse().setResponseCode(200).setBody("ex3");
}
return new MockResponse().setResponseCode(404);
}
};
mockBackEnd.setDispatcher(dispatcher);
}
@AfterAll
static void tearDown() throws IOException {
mockBackEnd.shutdown();
}
@Test
public void myTest() {
Map<String, String> map = new HashMap<>();
map.put("x-host", "api-v1.p.com");
when(settingsService.getSettingsMap()).thenReturn(map);
service.update();
verify(service, atLeastOnce()).update();
}
}
@Testcontainers
@SpringBootTest(类=MyApplication.class)
公共类服务测试{
公共静态MockWebServer mockBackEnd;
公共静态最终媒体类型JSON
=MediaType.parse(“application/json;charset=utf-8”);
@容器
私有静态最终PostgreSQLContainer=新的PostgreSQLContainer();
@蚕豆
私人服务;
@蚕豆
私人设置服务设置服务;
@以前
静态void setUp()引发IOException{
mockBackEnd=新的MockWebServer();
mockBackEnd.url(“https://api-v1.p.com/v2/country/2020");
最终调度程序=新调度程序(){
@凌驾
公共MockResponse分派(RecordedRequest请求)引发InterruptedException{
开关(request.getPath()){
案例“api-v1.p.com”:
返回新的MockResponse().setResponseCode(200).setBody(“ex1”);
案例“/v2/country/2020”:
返回新的MockResponse().setResponseCode(200).setBody(“ex2”);
案例“/:
返回新的MockResponse().setResponseCode(200).setBody(“ex3”);
}
返回新的MockResponse().setResponseCode(404);
}
};
mockBackEnd.setDispatcher(dispatcher);
}
@毕竟
静态void tearDown()引发IOException{
mockBackEnd.shutdown();
}
@试验
公共无效myTest(){
Map Map=newhashmap();
map.put(“x-host”、“api-v1.p.com”);
当(setingsService.getSettingsMap())。然后返回(map);
service.update();
验证(服务,至少一次()).update();
}
}
我的测试没有错误。虽然我没有被服务方式所吸引
当我做@自动连线私人服务时代码>我收到一个与访问外部资源相关的错误
如何修复测试,以便在访问url=时测试正确启动