forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONBehaviourTest.java
More file actions
62 lines (53 loc) · 2.14 KB
/
JSONBehaviourTest.java
File metadata and controls
62 lines (53 loc) · 2.14 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
package org.skyscreamer.jsonassert;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.json.JSONException;
import org.junit.Test;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.skyscreamer.jsonassert.Behavior.STRICT;
import static org.skyscreamer.jsonassert.Customization.customization;
import static org.skyscreamer.jsonassert.JSONCompare.compareJSON;
public class JSONBehaviourTest {
String actual = "{\"first\":\"actual\", \"second\":1}";
String expected = "{\"first\":\"expected\", \"second\":1}";
String deepActual = "{\n" +
" \"outer\":\n" +
" {\n" +
" \"inner\":\n" +
" {\n" +
" \"value\": \"actual\",\n" +
" \"otherValue\": \"foo\"\n" +
" }\n" +
" }\n" +
"}";
String deepExpected = "{\n" +
" \"outer\":\n" +
" {\n" +
" \"inner\":\n" +
" {\n" +
" \"value\": \"expected\",\n" +
" \"otherValue\": \"foo\"\n" +
" }\n" +
" }\n" +
"}";
EqualsComparator<Object> comparator = new EqualsComparator<Object>() {
@Override
public boolean equal(Object o1, Object o2) {
return o1.toString().equals("actual") && o2.toString().equals("expected");
}
};
@Test
public void whenPathMatchesInCustomizationThenCallCustomMatcher() throws JSONException {
Behavior behavior = STRICT.with(customization("first", comparator));
JSONCompareResult result = compareJSON(expected, actual, behavior);
assertTrue(result.getMessage(), result.passed());
}
@Test
public void whenDeepPathMatchesCallCustomMatcher() throws JSONException {
Behavior behavior = STRICT.with(customization("outer.inner.value", comparator));
JSONCompareResult result = compareJSON(deepExpected, deepActual, behavior);
assertTrue(result.getMessage(), result.passed());
}
}