forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
130 lines (114 loc) · 6 KB
/
index.html
File metadata and controls
130 lines (114 loc) · 6 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!doctype html>
<html>
<head>
<title>JSONAssert - Write JSON Unit Tests with Less Code - Introduction</title>
<meta name="description" content="Great for testing REST interfaces, JSONassert greatly simplifies testing JSON results in unit tests." />
<meta name="keywords" content="jsonassert,json unit test,rest unit test,json junit,rest junit" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link href="css/style.css" rel="stylesheet"/>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-33062731-1']);
_gaq.push(['_setDomainName', 'skyscreamer.org']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<header>
<h1>JSONassert</h1>
<h2>a <a href="http://skyscreamer.org">Skyscreamer</a> project</h2>
</header>
<br clear="all" />
<nav>
<ul>
<li><a class="intro" href="./">Introduction</a></li>
<li><a class="cookbook" href="cookbook.html">Cookbook</a></li>
<li><a class="quickstart" href="quickstart.html">Quickstart</a></li>
<li><a class="javadoc" href="apidocs/index.html">Javadoc</a></li>
<li><a class="download" href="https://github.com/skyscreamer/jsonassert/downloads">Download</a></li>
<li><a class="contrib" href="https://github.com/skyscreamer/jsonassert"> </a></li>
</ul>
</nav>
<section>
<a name="intro"></a>
<h2>Introduction</h2>
<p class="emphasize">Write JSON unit tests in less code. Great for testing REST interfaces.</p>
<p><br/>Code JSON tests as if you are comparing a string. Under the covers, JSONassert converts your string into a JSON object and compares the logical structure and data with the actual JSON. When strict is set to false (recommended), it forgives reordering data and extending results (as long as all the expected elements are there), making tests less brittle.</p>
<p><br/>Supported test frameworks:</p>
<ul>
<li><a href="http://junit.org" target="_blank">JUnit</a></li>
</ul>
<p><br/>The current version of JSONassert is 1.1.1</p>
<h2>Examples</h2>
<div class="example">
<p>In JSONassert, you write and maintain something like this:</p>
<blockquote>
<a>JSONObject data = getRESTData("/friends/367.json");</a><br/>
<a>String expected = "{friends:[{id:123,name:\"Corby Page\"},{id:456,name:\"Carter Page\"}]}";</a><br/>
<a>JSONAssert.assertEquals(expected, data, false);</a><br/>
</blockquote>
</div>
<div class="example">
<p>... instead of all this:</p>
<blockquote class="strikethrough">
<a>JSONObject data = getRESTData("/friends/367.json");</a><br/>
<a>Assert.assertTrue(data.has("friends"));</a><br/>
<a>Object friendsObject = data.get("friends");</a><br/>
<a>Assert.assertTrue(friendsObject instanceof JSONArray);</a><br/>
<a>JSONArray friends = (JSONArray) friendsObject;</a><br/>
<a>Assert.assertEquals(2, data.length());</a><br/>
<a>JSONObject friend1Obj = friends.getJSONObject(data.get(0));</a><br/>
<a>Assert.true(friend1Obj.has("id"));</a><br/>
<a>Assert.true(friend1Obj.has("name"));</a><br/>
<a>JSONObject friend2Obj = friends.getJSONObject(data.get(1));</a><br/>
<a>Assert.true(friend2Obj.has("id"));</a><br/>
<a>Assert.true(friend2Obj.has("name"));</a><br/>
<a>if ("Carter Page".equals(friend1Obj.getString("name"))) {</a><br/>
<a> Assert.assertEquals(123, friend1Obj.getInt("id"));</a><br/>
<a> Assert.assertEquals("Corby Page", friend2Obj.getString("name"));</a><br/>
<a> Assert.assertEquals(456, friend2Obj.getInt("id"));</a><br/>
<a>}</a><br/>
<a>else if ("Corby Page".equals(friend1Obj.getString("name"))) {</a><br/>
<a> Assert.assertEquals(456, friend1Obj.getInt("id"));</a><br/>
<a> Assert.assertEquals("Carter Page", friend2Obj.getString("name"));</a><br/>
<a> Assert.assertEquals(123, friend2Obj.getInt("id"));</a><br/>
<a>}</a><br/>
<a>else {</a><br/>
<a> Assert.fail("Expected either Carter or Corby, Got: " + friend1Obj.getString("name"));</a><br/>
<a>}</a><br/>
</blockquote>
</div>
<h2>Error Messages</h2>
<div class="example">
<p>We tried to make error messages easy to understand. This is really important, since it gets hard for the eye to pick out the difference, particularly in long JSON strings. For example:</p>
<blockquote>
<a>String expected = "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"<span class="emphasize">bird</span>\",\"fish\"]}],pets:[]}";</a><br/>
<a>String actual = "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"<span class="emphasize">cat</span>\",\"fish\"]}],pets:[]}"</a><br/>
<a>JSONAssert.assertEquals(expected, actual, false);</a><br/>
</blockquote>
</div>
<div class="example">
<p>... returns the following:</p>
<blockquote>
<a>friends[id=3].pets[]: Expected bird, but not found ; friends[id=3].pets[]: Contains cat, but not expected</a><br/>
</blockquote>
</div>
... which tells you that the pets array under the friend where id=3 was supposed to contain "bird", but had "cat" instead.
(Maybe the cat ate the bird?)
</section>
<section>
<a name="contact"></a>
<h2>Contact</h2>
<p>
This is open source so if you want to help out whether by submitting code, design, suggestions, feedback,
or feature requests, we appreciate whatever you can contribute. Contact us at
<a href="mailto:jsonassert-dev@skyscreamer.org">jsonassert-dev@skyscreamer.org</a> with questions or ideas.
</p>
</section>
</body>
</html>