You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: karate-mock-servlet/README.md
+5-16
Original file line number
Diff line number
Diff line change
@@ -22,22 +22,14 @@ So yes, you can test HTTP web-services with the same ease that you expect from t
22
22
```
23
23
24
24
## Switching the HTTP Client
25
-
Karate actually allows you to switch the implementation of the Karate [`HttpClient`](../karate-core/src/main/java/com/intuit/karate/http/HttpClient.java)even *during* a test. For mocking a servlet container, you don't need to implement it from scratch and you just need to over-ride one or two methods of the mock-implementation that Karate provides.
25
+
You can completely customize the HTTP client used by Karate by implementing the [`HttpClient`](../karate-core/src/main/java/com/intuit/karate/http/HttpClient.java)interface which happens to be very simple.
26
26
27
-
> If you need to create a completely new `HttpClient` implementation from scratch, the [`MockHttpClient`](src/main/java/com/intuit/karate/mock/servlet/MockHttpClient.java) 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.
27
+
If you need to create a completely new `HttpClient` implementation from scratch, the [`MockHttpClient`](src/main/java/com/intuit/karate/mock/servlet/MockHttpClient.java) 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.
28
28
29
-
Let's take a closer look at the following [`configure`](https://github.com/intuit/karate#configure) keys:
30
-
31
-
Key | Type | Description
32
-
------ | ---- | ---------
33
-
`httpClientClass` | string | The class name of the implementation you wish to use. By default the Karate Apache (or Jersey) HTTP client is used. You need a zero-argument constructor. If you need more control over construction, refer to the next row.
34
-
`httpClientInstance` | Java Object | A fully constructed instance of an `HttpClient` implementation. Useful if you need more dynamic control over things like dependency-injection.
35
-
`userDefined` | JSON | You normally would not need this general-purpose extension mechanism where you can pass custom JSON data to the HTTP Client instance via the [`configure`](https://github.com/intuit/karate#configure) keyword. Refer to [this test](../karate-core/src/test/java/com/intuit/karate/http/HttpClientTest.java) for an idea of how you can use this for advanced needs, such as if you wanted to customize your HTTP Client implementation *during* a test.
29
+
Karate defaults to the [`ApacheHttpClient`](../karate-core/src/main/java/com/intuit/karate/http/ApacheHttpClient.java), and to change this for a test-run, you can set the [`HttpClientFactory`](../karate-core/src/main/java/com/intuit/karate/http/HttpClientFactory.java) using the [`Runner`](../karate-core/src/main/java/com/intuit/karate/Runner.java) "builder" API.
36
30
37
31
## Mocking Your Servlet
38
-
You only need to over-ride two methods:
39
-
*`Servlet getServlet(HttpRequestBuilder request)`
40
-
*`ServletContext getServletContext()`
32
+
Creating a `Servlet` and `ServletContext` instance is up to you and here is where you would manage configuration for your web-application. And then you can implement `HttpClientFactory` by using the `MockHttpClient` code provide by Karate.
41
33
42
34
Once you refer to the following examples, you should be able to get up and running for your project.
43
35
* Spring MVC Dispatcher Servlet
@@ -47,10 +39,7 @@ Once you refer to the following examples, you should be able to get up and runni
47
39
48
40
Note that the first example above tests the whole of the [`karate-demo`](../karate-demo) Spring Boot application, and using a [parallel runner](src/test/java/demo/MockSpringMvcServletTest.java)
49
41
50
-
## Configuration
51
-
Everything is typically tied together in [bootstrap configuration](https://github.com/intuit/karate#configuration). If you do this right, your `*.feature` files can be re-used for mock as well as *real* integration tests.
52
-
53
-
Use the test configuration for this `karate-mock-servlet` project as a reference: [`karate-config.js`](src/test/java/karate-config.js)
42
+
If you structure your tests propertly, your `*.feature` files can be re-used for mock as well as *real* integration tests.
54
43
55
44
## Limitations
56
45
Most teams would not run into these, but if you do, please [consider contributing](https://github.com/intuit/karate/projects/3#card-22529274) !
0 commit comments