Example unit test for the article "Count with JsonPath"
This commit is contained in:
parent
ce4b42926f
commit
8ebe69f176
21
json-path/src/main/resources/online_store.json
Normal file
21
json-path/src/main/resources/online_store.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"items": {
|
||||
"book": [
|
||||
{
|
||||
"author": "Arthur Conan Doyle",
|
||||
"title": "Sherlock Holmes",
|
||||
"price": 8.99
|
||||
},
|
||||
{
|
||||
"author": "J. R. R. Tolkien",
|
||||
"title": "The Lord of the Rings",
|
||||
"isbn": "0-395-19395-8",
|
||||
"price": 22.99
|
||||
}
|
||||
],
|
||||
"bicycle": {
|
||||
"color": "red",
|
||||
"price": 19.95
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.baeldung.jsonpath.introduction;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
|
||||
import net.minidev.json.JSONArray;
|
||||
|
||||
public class JsonPathUnitTest {
|
||||
|
||||
private static String json;
|
||||
private static File jsonFile = new File("src/main/resources/online_store.json");
|
||||
|
||||
private static String readFile(File file, Charset charset) throws IOException {
|
||||
return new String(Files.readAllBytes(file.toPath()), charset);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws IOException {
|
||||
json = readFile(jsonFile, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMatchCountOfObjects() {
|
||||
Map<String, String> objectMap = JsonPath.read(json, "$");
|
||||
assertEquals(1, objectMap.keySet()
|
||||
.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMatchCountOfArrays() {
|
||||
JSONArray jsonArray = JsonPath.read(json, "$.items.book[*]");
|
||||
assertEquals(2, jsonArray.size());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user