samedi 3 octobre 2020

stub response for okhttp

I am writing a test. My service looks like:

@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());
        }
    }
}

And my test:

@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();
    }
}

My test works without error. Though I don't get caught up in the service method.

When i do @Autowired private Service service; i am getting an error related to accessing an external resource.

How can I fix my test so that when accessing url = https://api-v1.p.com/v2/country/2020 the test starts correctly ?

Aucun commentaire:

Enregistrer un commentaire