Skip to content

Commit b4513d8

Browse files
committed
Merge pull request skyscreamer#7 from hertzsprung/comparison-result-fields
Expose field, actual and expected values through JSONCompareResult
2 parents 9771c19 + 3b854c0 commit b4513d8

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/main/java/org/skyscreamer/jsonassert/JSONCompareResult.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
public class JSONCompareResult {
77
private boolean _success;
88
private String _message;
9+
private String _field;
10+
private Object _expected;
11+
private Object _actual;
912

1013
/**
1114
* Default constructor.
@@ -42,6 +45,45 @@ public boolean failed() {
4245
public String getMessage() {
4346
return _message;
4447
}
48+
49+
/**
50+
* Actual field value
51+
*
52+
* @return a {@code JSONObject}, {@code JSONArray} or other {@code Object}
53+
* instance, or {@code null} if the comparison did not fail on a
54+
* particular field
55+
*/
56+
public Object getActual() {
57+
return _actual;
58+
}
59+
60+
/**
61+
* Expected field value
62+
*
63+
* @return a {@code JSONObject}, {@code JSONArray} or other {@code Object}
64+
* instance, or {@code null} if the comparison did not fail on a
65+
* particular field
66+
*/
67+
public Object getExpected() {
68+
return _expected;
69+
}
70+
71+
/**
72+
* Check if comparison failed on a particular field
73+
*/
74+
public boolean isFailureOnField() {
75+
return _field != null;
76+
}
77+
78+
/**
79+
* Dot-separated path the the field that failed comparison
80+
*
81+
* @return a {@code String} instance, or {@code null} if the comparison did
82+
* not fail on a particular field
83+
*/
84+
public String getField() {
85+
return _field;
86+
}
4587

4688
protected void fail(String message) {
4789
_success = false;
@@ -54,13 +96,20 @@ protected void fail(String message) {
5496
}
5597

5698
protected void fail(String field, Object expected, Object actual) {
99+
this._field = field;
100+
this._expected = expected;
101+
this._actual = actual;
102+
fail(formatFailureMessage(field, expected, actual));
103+
}
104+
105+
private String formatFailureMessage(String field, Object expected, Object actual) {
57106
StringBuffer message= new StringBuffer();
58107
message.append(field);
59108
message.append("\nExpected: ");
60109
message.append(expected + "");
61110
message.append("\n got: ");
62111
message.append(actual + "");
63112
message.append("\n");
64-
fail(message.toString());
113+
return message.toString();
65114
}
66115
}

src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ public void testArrayOfArraysStrict() throws JSONException {
135135
public void testArrayOfArrays() throws JSONException {
136136
testPass("{id:1,stuff:[[4,3],[3,2],[],[1,2]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", false);
137137
}
138+
139+
@Test
140+
public void testFieldMismatch() throws JSONException {
141+
JSONCompareResult result = JSONCompare.compareJSON("{name:\"Pat\"}", "{name:\"Sue\"}", STRICT);
142+
Assert.assertEquals("Pat", result.getExpected());
143+
Assert.assertEquals("Sue", result.getActual());
144+
Assert.assertEquals("name", result.getField());
145+
}
138146

139147
@Test
140148
public void testNullProperty() throws JSONException {

0 commit comments

Comments
 (0)