Skip to content

Commit 417cef9

Browse files
committed
Expose field, actual and expected values through JSONCompareResult
1 parent 3d25774 commit 417cef9

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-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
}

0 commit comments

Comments
 (0)