Skip to content

Commit 340d1f5

Browse files
committed
more documentation about mocks
1 parent 9696ef7 commit 340d1f5

File tree

2 files changed

+147
-3
lines changed

2 files changed

+147
-3
lines changed

mkdocs/docs/mocks.md

+85-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A series of mocks for use with Unirest for unit testing. Mocked clients will not
1111
C -->|Normal Runtime| F[Java HttpClient]
1212
```
1313

14-
## Expectations
14+
## Expecting Requests
1515
You can either mock the default static implementation or a per instance implementation. In both cases you need to register the mock with Unirest.
1616

1717
### Static Mocking
@@ -49,7 +49,7 @@ class MyTest {
4949
}
5050
```
5151

52-
## Multiple Expects
52+
### Multiple Expects
5353
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.
5454

5555
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
8686
}
8787
```
8888

89-
## Verifying Expects
89+
### Verifying Expects
9090
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.
9191

9292
```java
@@ -120,6 +120,88 @@ If you want to get more specific we can keep around our expectations and validat
120120
}
121121
```
122122

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());
181+
assertEquals("active", response.getHeaders().getFirst("x-zombo-brewing"));
182+
}
183+
```
184+
185+
### Responses with JSON Bodies
186+
The mocking framework will use whatever ObjectMapper is configured with Unirest to marshall Pojos to expected responses.
187+
188+
```java
189+
190+
static class Teapot { public String brewstatus = "on"; }
191+
@Test
192+
void pojos() {
193+
MockClient mock = MockClient.register();
194+
195+
mock.expect(HttpMethod.GET, "http://zombo.com")
196+
.thenReturn(new Teapot());
197+
198+
var response = Unirest.get("http://zombo.com").asString();
199+
200+
assertEquals("{\"brewstatus\":\"on\"}", response.getBody());
201+
}
202+
```
203+
204+
123205

124206

125207

unirest-modules-mocks/src/test/java/kong/tests/DocumentationExamplesTest.java

+62
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,66 @@ void multipleValidates(){
122122
zombo.verify();
123123
homestar.verify(Times.never());
124124
}
125+
126+
@Test
127+
void simpleBody() {
128+
MockClient mock = MockClient.register();
129+
130+
mock.expect(HttpMethod.POST, "http://zombo.com")
131+
.body("I can do anything? Anything at all?")
132+
.thenReturn()
133+
.withStatus(201);
134+
135+
assertEquals(201,
136+
Unirest.post("http://zombo.com").body("I can do anything? Anything at all?").asEmpty().getStatus()
137+
);
138+
}
139+
140+
@Test
141+
void formParams() {
142+
MockClient mock = MockClient.register();
143+
144+
mock.expect(HttpMethod.POST, "http://zombo.com")
145+
.body(FieldMatcher.of("foo", "bar",
146+
"baz", "qux"))
147+
.thenReturn()
148+
.withStatus(201);
149+
150+
assertEquals(201,
151+
Unirest.post("http://zombo.com")
152+
.field("foo", "bar")
153+
.field("baz", "qux")
154+
.asEmpty().getStatus()
155+
);
156+
}
157+
158+
@Test
159+
void response() {
160+
MockClient mock = MockClient.register();
161+
162+
mock.expect(HttpMethod.GET, "http://zombo.com")
163+
.thenReturn("Care for some tea mum?")
164+
.withHeader("x-zombo-brewing", "active")
165+
.withStatus(418, "I am a teapot");
166+
167+
var response = Unirest.get("http://zombo.com").asString();
168+
169+
assertEquals(418, response.getStatus());
170+
assertEquals("I am a teapot", response.getStatusText());
171+
assertEquals("Care for some tea mum?", response.getBody());
172+
assertEquals("active", response.getHeaders().getFirst("x-zombo-brewing"));
173+
}
174+
175+
static class Teapot { public String brewstatus = "on"; }
176+
@Test
177+
void pojos() {
178+
MockClient mock = MockClient.register();
179+
180+
mock.expect(HttpMethod.GET, "http://zombo.com")
181+
.thenReturn(new Teapot());
182+
183+
var response = Unirest.get("http://zombo.com").asString();
184+
185+
assertEquals("{\"brewstatus\":\"on\"}", response.getBody());
186+
}
125187
}

0 commit comments

Comments
 (0)