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: mkdocs/docs/mocks.md
+85-3
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ A series of mocks for use with Unirest for unit testing. Mocked clients will not
11
11
C -->|Normal Runtime| F[Java HttpClient]
12
12
```
13
13
14
-
## Expectations
14
+
## Expecting Requests
15
15
You can either mock the default static implementation or a per instance implementation. In both cases you need to register the mock with Unirest.
16
16
17
17
### Static Mocking
@@ -49,7 +49,7 @@ class MyTest {
49
49
}
50
50
```
51
51
52
-
## Multiple Expects
52
+
###Multiple Expects
53
53
HTTP requests can have many parts, some of which are automatic or at least uninteresting from the standpoint of testing. This means that setting up an exact expectation to match the request exactly can be tedious.
54
54
55
55
You can register as many expects as you like. Which one is used for any particular invocation of Unirest depends on a points system. Each expectation is evaluated and given points for each positive part while any negative part immediately discards the expect. The expectation that has the most points "wins".
@@ -86,7 +86,7 @@ In this example, we have three expectations, one doesn't match at all. and two o
86
86
}
87
87
```
88
88
89
-
## Verifying Expects
89
+
###Verifying Expects
90
90
Sometimes we only want to know that the needful was done. In this case we can validate our mock. The simplest way is to call verifyAll which will validate that all expects were called at least once.
91
91
92
92
```java
@@ -120,6 +120,88 @@ If you want to get more specific we can keep around our expectations and validat
120
120
}
121
121
```
122
122
123
+
### Expected Body Matching
124
+
You can match specific body content with some limitations. Complex bodies must implement ```BodyMatcher```. There are two implementations available: ```EqualsBodyMatcher``` which is used for simple equality and ```FieldMatcher``` which is for form params. You can create your own.
125
+
126
+
#### Simple Bodies
127
+
```java
128
+
@Test
129
+
void simpleBody() {
130
+
MockClient mock =MockClient.register();
131
+
132
+
mock.expect(HttpMethod.POST, "http://zombo.com")
133
+
.body("I can do anything? Anything at all?")
134
+
.thenReturn()
135
+
.withStatus(201);
136
+
137
+
assertEquals(201,
138
+
Unirest.post("http://zombo.com").body("I can do anything? Anything at all?").asEmpty().getStatus()
139
+
);
140
+
}
141
+
```
142
+
143
+
#### Form Params
144
+
```java
145
+
@Test
146
+
void formParams() {
147
+
MockClient mock =MockClient.register();
148
+
149
+
mock.expect(HttpMethod.POST, "http://zombo.com")
150
+
.body(FieldMatcher.of("foo", "bar",
151
+
"baz", "qux"))
152
+
.thenReturn()
153
+
.withStatus(201);
154
+
155
+
assertEquals(201,
156
+
Unirest.post("http://zombo.com")
157
+
.field("foo", "bar")
158
+
.field("baz", "qux")
159
+
.asEmpty().getStatus()
160
+
);
161
+
}
162
+
```
163
+
164
+
## Expected Responses
165
+
You can set all properties of a response.
166
+
```java
167
+
@Test
168
+
void response() {
169
+
MockClient mock =MockClient.register();
170
+
171
+
mock.expect(HttpMethod.GET, "http://zombo.com")
172
+
.thenReturn("Care for some tea mum?")
173
+
.withHeader("x-zombo-brewing", "active")
174
+
.withStatus(418, "I am a teapot");
175
+
176
+
var response =Unirest.get("http://zombo.com").asString();
177
+
178
+
assertEquals(418, response.getStatus());
179
+
assertEquals("I am a teapot", response.getStatusText());
180
+
assertEquals("Care for some tea mum?", response.getBody());
0 commit comments