-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathClientTest.java
More file actions
89 lines (76 loc) · 3.48 KB
/
ClientTest.java
File metadata and controls
89 lines (76 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* Copyright 2009-2019 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.javacrumbs.jsonunit.spring.testit;
import static net.javacrumbs.jsonunit.spring.JsonUnitRequestMatchers.json;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
class ClientTest {
private static final String URI = "/sample";
private final RestTemplate restTemplate = new RestTemplate();
private final MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
private final String jsonResponse = "{\"response\": \"€\"}";
private final String json = "{\"test\": 1}";
@Test
void shouldAssertClient() {
mockServer
.expect(requestTo(URI))
.andExpect(json().isEqualTo(json))
.andRespond(withSuccess(jsonResponse, MediaType.APPLICATION_JSON));
assertThat(restTemplate.postForEntity(URI, json, String.class).getBody())
.isEqualTo(jsonResponse);
}
@Test
void shouldFailOnMismatch() {
mockServer
.expect(requestTo(URI))
.andExpect(json().isEqualTo("[]"))
.andRespond(withSuccess(jsonResponse, MediaType.APPLICATION_JSON));
assertThatThrownBy(() -> restTemplate.postForEntity(URI, json, String.class))
.hasMessage(
"""
JSON documents are different:
Different value found in node "", expected: <[]> but was: <{"test":1}>.
""");
}
@Test
void shouldAssertClientComplex() {
mockServer
.expect(requestTo(URI))
.andExpect(method(HttpMethod.POST))
.andExpect(json().node("test").withTolerance(0.1).isEqualTo(0.99))
.andRespond(withSuccess(jsonResponse, MediaType.APPLICATION_JSON));
assertThat(restTemplate.postForEntity(URI, json, String.class).getBody())
.isEqualTo(jsonResponse);
}
@Test
void shouldAssertClientJsonPath() {
mockServer
.expect(requestTo(URI))
.andExpect(method(HttpMethod.POST))
.andExpect(json().inPath("$.test").isEqualTo(1))
.andRespond(withSuccess(jsonResponse, MediaType.APPLICATION_JSON));
assertThat(restTemplate.postForEntity(URI, json, String.class).getBody())
.isEqualTo(jsonResponse);
}
}