javaspring-bootkaratejava-11spring-boot-test

how to configure karate to use FeignClient or RestTemplate in place of ApacheHttpClient


versions: Karate 1.3.1, maven 3.8, Java 11

Below is how i'm invoking karate test

@SpringBootTest
@ActiveProfiles(value = "${spring.profiles.active:dev}")
public class ControllerIntegrationTest {

    @Value("${service-ui-rest.url}")
    private String serviceUiRestUrl;


    @Karate.Test
    Karate testThis(){
        System.setProperty("file.encoding", "UTF-8");
        return Karate
                .run("classpath:feature")
                .systemProperty("service-ui-rest.url",serviceUiRestUrl)
                .systemProperty("test.type","integration");
    }
}

While debugging, I discover when the following logic get call HttpClientFactory returns ApacheHttpClient::new

https://github.com/karatelabs/karate/blob/628428c5f94772a40ae6f330f806388e6dfb0118/karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java#L1238

https://github.com/karatelabs/karate/blob/628428c5f94772a40ae6f330f806388e6dfb0118/karate-core/src/main/java/com/intuit/karate/http/HttpClientFactory.java#L33

and wonder how to configure karate to use the bean for: FeignClient or RestTemplate that I have

i found something similar at : https://github.com/gtnicol/karate-okhttp-signpost/blob/master/src/main/resources/karate-http.properties , but its using karate 0.9.1

any pointers will be highly appreciated


Solution

  • This should answer your question: https://github.com/karatelabs/karate/tree/v1.3.0/karate-mock-servlet#switching-the-http-client

    You can completely customize the HTTP client used by Karate by implementing the HttpClient interface which happens to be very simple.

    If you need to create a completely new HttpClient implementation from scratch, the MockHttpClient is a good reference. There are many possibilities here, you can add support for other HTTP clients besides Apache and Jersey, or mock a stack that is not based on Java servlets.

    Karate defaults to the ApacheHttpClient, and to change this for a test-run, you can set the HttpClientFactory using the Runner "builder" API.

    This bit of documentation was lost when the karate-mock-servlet was deprecated in 1.4.0, but hopefully now this post on stack-overflow can be the reference that people need. That said, I think you may be expecting something "plug and play" but no, there will be some work involved to write an adapter. Teams never need to do this though, the whole point of testing using an HTTP client is to simulate how end-users can consume your API.