Skip to content

Commit acc3d0d

Browse files
committed
Add cookbook and quickstart
1 parent b6c209f commit acc3d0d

File tree

2 files changed

+219
-0
lines changed

2 files changed

+219
-0
lines changed

cookbook.html

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>JSONAssert - Write JSON Unit Tests with Less Code - Cookbook</title>
5+
<meta name="description" content="Great for testing REST interfaces, JSONassert greatly simplifies testing JSON results in unit tests." />
6+
<meta name="keywords" content="jsonassert,json unit test,rest unit test,json junit,rest junit" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
8+
<link href="css/style.css" rel="stylesheet"/>
9+
<script type="text/javascript">
10+
11+
var _gaq = _gaq || [];
12+
_gaq.push(['_setAccount', 'UA-33062731-1']);
13+
_gaq.push(['_setDomainName', 'skyscreamer.org']);
14+
_gaq.push(['_trackPageview']);
15+
16+
(function() {
17+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
18+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
19+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
20+
})();
21+
22+
</script>
23+
</head>
24+
<body>
25+
<header>
26+
<h1>JSONassert</h1>
27+
<h2>a <a href="http://skyscreamer.org">Skyscreamer</a> project</h2>
28+
</header>
29+
<br clear="all" />
30+
<nav>
31+
<ul>
32+
<li><a class="intro" href="./">Introduction</a></li>
33+
<li><a class="cookbook" href="">Cookbook</a></li>
34+
<li><a class="quickstart" href="quickstart.html">Quickstart</a></li>
35+
<li><a class="javadoc" href="javadoc/index.html">Javadoc</a></li>
36+
<li><a class="download" href="https://github.com/skyscreamer/jsonassert/downloads">Download</a></li>
37+
<li><a class="contrib" href="https://github.com/skyscreamer/jsonassert"> &nbsp; </a></li>
38+
</ul>
39+
</nav>
40+
<section>
41+
<a name="cookbook"></a>
42+
<h2>Cookbook</h2>
43+
44+
<p>Assertion parameters can be a java.lang.String with JSON data, an org.json.JSONObject, or an org.json.JSONArray.
45+
For readability, we'll use strings in the following examples.</p>
46+
47+
<div class="example">
48+
<p>A really simple example. Get a JSON object and test its ID:</p>
49+
<blockquote>
50+
<a>@Test</a><br/>
51+
<a>public void testSimple() {</a><br/>
52+
<a> &nbsp; String result = getJSONResult("/user/1.json");</a><br/>
53+
<a> &nbsp; JSONAssert.assertEquals("{id:1}", result, false); <span class="emphasize italics">// Pass</span></a><br/>
54+
<a>}</a><br/>
55+
</blockquote>
56+
</div>
57+
58+
<div class="example">
59+
<p>If you enable <span class="italics">strictMode</span>, then extended fields fail:</p>
60+
<blockquote>
61+
<a>String result = "{id:1,name:\"Juergen\"}";</a><br/>
62+
<a>JSONAssert.assertEquals("{id:1}", result, <span class="emphasize">false</span>); <span class="emphasize italics">// Pass</span></a><br/>
63+
<a>JSONAssert.assertEquals("{id:1}", result, <span class="emphasize">true</span>); <span class="emphasize italics">// Fail</span></a><br/>
64+
</blockquote>
65+
</div>
66+
67+
<div class="example">
68+
<p>Strict or not, field order does not matter:</p>
69+
<blockquote>
70+
<a>String result = "{id:1,name:\"Juergen\"}";</a><br/>
71+
<a>JSONAssert.assertEquals("{name:\"Juergen\",id:1}", result, true); <span class="emphasize italics">// Pass</span></a><br/>
72+
</blockquote>
73+
</div>
74+
75+
76+
<p>Because application interfaces are naturally extended as they mature, it is recommended that you
77+
default to leaving strict mode off, except in particular cases.</p>
78+
79+
<div class="example">
80+
<p>Arrays rules are different. If sequence is important, you can enable strict mode:</p>
81+
<blockquote>
82+
<a>String result = "[1,2,3,4,5]";</a><br/>
83+
<a>JSONAssert.assertEquals("[1,2,3,4,5]", result, true); <span class="emphasize italics">// Pass</span></a><br/>
84+
<a>JSONAssert.assertEquals("[5,3,2,1,4]", result, true); <span class="emphasize italics">// Fail</span></a><br/>
85+
</blockquote>
86+
</div>
87+
88+
<div class="example">
89+
<p>When strict mode is off, arrays can be in any order:</p>
90+
<blockquote>
91+
<a>String result = "[1,2,3,4,5]";</a><br/>
92+
<a>JSONAssert.assertEquals("[5,3,2,1,4]", result, false); <span class="emphasize italics">// Pass</span></a><br/>
93+
</blockquote>
94+
</div>
95+
96+
<div class="example">
97+
<p>Strict or not, arrays must match. They can't be "extended" like object fields can:</p>
98+
<blockquote>
99+
<a>String result = "[1,2,3,4,5]";</a><br/>
100+
<a>JSONAssert.assertEquals("[1,2,3,4,5]", result, false); <span class="emphasize italics">// Pass</span></a><br/>
101+
<a>JSONAssert.assertEquals("[1,2,3]", result, false); <span class="emphasize italics">// Fail</span></a><br/>
102+
<a>JSONAssert.assertEquals("[1,2,3,4,5,6]", result, false); <span class="emphasize italics">// Fail</span></a><br/>
103+
</blockquote>
104+
</div>
105+
106+
<p>The example so far are simple, but this will work for JSON objects of any size (per VM memory limits), depth,
107+
or complexity.</p>
108+
109+
<div class="example">
110+
<p>You can test arrays of arrays, loose/strict ordering constraints apply at all levels:</p>
111+
<blockquote>
112+
<a>String result = "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}";</a><br/>
113+
<a>JSONAssert.assertEquals("{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", result, true); <span class="emphasize italics">// Pass</span></a><br/>
114+
<a>JSONAssert.assertEquals("{id:1,stuff:[[4,3],[3,2],[],[1,2]]}", result, false); <span class="emphasize italics">// Pass</span></a><br/>
115+
</blockquote>
116+
</div>
117+
118+
<div class="example">
119+
<p>You can test arrays of arrays, loose/strict ordering constraints apply at all levels:</p>
120+
<blockquote>
121+
<a>String result = "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}";</a><br/>
122+
<a>JSONAssert.assertEquals("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}", result, true); <span class="emphasize italics">// Pass</span></a><br/>
123+
<a>JSONAssert.assertEquals("{name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[],id:1}", result, false); <span class="emphasize italics">// Pass</span></a><br/>
124+
</blockquote>
125+
</div>
126+
127+
<div class="example">
128+
<p>As you can see, tests work against any level of depth:</p>
129+
<blockquote>
130+
<a>String result = "{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}";</a><br/>
131+
<a>JSONAssert.assertEquals("{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}",
132+
result, true); <span class="emphasize italics">// Pass</span></a><br/>
133+
</blockquote>
134+
</div>
135+
136+
</body>
137+
</html>

quickstart.html

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>JSONAssert - Write JSON Unit Tests with Less Code - Quickstart</title>
5+
<meta name="description" content="Great for testing REST interfaces, JSONassert greatly simplifies testing JSON results in unit tests." />
6+
<meta name="keywords" content="jsonassert,json unit test,rest unit test,json junit,rest junit" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
8+
<link href="css/style.css" rel="stylesheet"/>
9+
<script type="text/javascript">
10+
11+
var _gaq = _gaq || [];
12+
_gaq.push(['_setAccount', 'UA-33062731-1']);
13+
_gaq.push(['_setDomainName', 'skyscreamer.org']);
14+
_gaq.push(['_trackPageview']);
15+
16+
(function() {
17+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
18+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
19+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
20+
})();
21+
22+
</script>
23+
</head>
24+
<body>
25+
<header>
26+
<h1>JSONassert</h1>
27+
<h2>a <a href="http://skyscreamer.org">Skyscreamer</a> project</h2>
28+
</header>
29+
<br clear="all" />
30+
<nav>
31+
<ul>
32+
<li><a class="intro" href="./">Introduction</a></li>
33+
<li><a class="cookbook" href="cookbook.html">Cookbook</a></li>
34+
<li><a class="quickstart" href="">Quickstart</a></li>
35+
<li><a class="javadoc" href="javadoc/index.html">Javadoc</a></li>
36+
<li><a class="download" href="https://github.com/skyscreamer/jsonassert/downloads">Download</a></li>
37+
<li><a class="contrib" href="https://github.com/skyscreamer/jsonassert"> &nbsp; </a></li>
38+
</ul>
39+
</nav>
40+
<section>
41+
<a name="intro"></a>
42+
<h2>Quick Start</h2>
43+
44+
<p>To use, <a href="https://github.com/skyscreamer/jsonassert/downloads">download the JAR</a> or
45+
add the following to your project's pom.xml:</p>
46+
47+
<div class="example">
48+
<blockquote>
49+
<a>&lt;dependency&gt;</a><br/>
50+
<a> &nbsp; &lt;groupId&gt;org.skyscreamer&lt;/groupId&gt;</a><br/>
51+
<a> &nbsp; &lt;artifactId&gt;jsonassert&lt;/artifactId&gt;</a><br/>
52+
<a> &nbsp; &lt;version&gt;1.0.0&lt;/version&gt;</a><br/>
53+
<a>&lt;/dependency&gt;</a><br/>
54+
</blockquote>
55+
</div>
56+
57+
<p>Syntax is simple, and similar to JUnit Assert:</p>
58+
<div class="example">
59+
<blockquote>
60+
<a>JSONAssert.assertEquals(<span class="italics">expectedJSON</span>, <span class="italics">actualJSON</span>, <span class="italics">strictMode</span>);</a><br/>
61+
</blockquote>
62+
</div>
63+
64+
<p>Add JSONassert tests within existing JUnit tests, just like you would add a standard Assert:</p>
65+
66+
<div class="example">
67+
<blockquote>
68+
<a>@Test</a><br/>
69+
<a>public void testGetUser() {</a><br/>
70+
<a> &nbsp; Assert.assertTrue(_restService.isEnabled());</a><br/>
71+
<a> &nbsp; String result = _restService.get("/user/123.json");</a><br/>
72+
<a class="emphasize"> &nbsp; JSONAssert.assertEquals("{id:123,name:\"Joe\"}", result, false);</a><br/>
73+
<a>}</a><br/>
74+
</blockquote>
75+
</div>
76+
77+
<p>It is recommended that you leave <span class="italics">strictMode</span>
78+
off, so your will be tests less brittle.
79+
Turn it on if you need to enforce a particular order for arrays, or if you want to
80+
ensure that the actual JSON does not have any fields beyond what's expected.</p>
81+
</body>
82+
</html>

0 commit comments

Comments
 (0)