如何写junit测试用例解析jsonresponse?
How to write junit test case parsing jsonresponse?
我正在尝试编写一个测试用例,其中的条件是我必须读取一个 json 文件,然后如果 valuesdata 为真,则它必须具有 values 属性,当它为假时,它应该具有 sql属性
{
"data": [
{
"valuesdata": true,
"values": [
{
"id": "1"
}
]
},
{
"valuesdata": false,
"sql": "select * from data"
}
]
}
这就是我想写的,感谢任何帮助
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class Test{
@Test
@DisplayName("If Json valuesdata is true it should have values attribute")
public void dropdownJsonValueIsStaticTrue() throws Exception {
String content = new String(Files.readAllBytes(Paths.get(input_file_path)));
JSONObject jsonObjects = new JSONObject(content);
jsonObjects.getJSONArray("data").getJSONObject(0).getBoolean("valuesdata");
}
}
你可以考虑:
@Test
public void dropdownJsonValueIsStaticTrue() throws Exception {
String content = new String(Files.readAllBytes(Paths.get(input_file_path)));
JSONObject jsonObjects = new JSONObject(content);
JSONArray datas = jsonObjects.getJSONArray("data");
for (int i = 0 ; i < datas.length(); i++) {
JSONObject data = datas.getJSONObject(i);
boolean valuesdata = data.getBoolean("valuesdata");
if(valuesdata) {
assertTrue(data.getJSONArray("values") != null);
} else {
assertTrue(data.getString("sql") != null);
}
}
}
我正在尝试编写一个测试用例,其中的条件是我必须读取一个 json 文件,然后如果 valuesdata 为真,则它必须具有 values 属性,当它为假时,它应该具有 sql属性
{
"data": [
{
"valuesdata": true,
"values": [
{
"id": "1"
}
]
},
{
"valuesdata": false,
"sql": "select * from data"
}
]
}
这就是我想写的,感谢任何帮助
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class Test{
@Test
@DisplayName("If Json valuesdata is true it should have values attribute")
public void dropdownJsonValueIsStaticTrue() throws Exception {
String content = new String(Files.readAllBytes(Paths.get(input_file_path)));
JSONObject jsonObjects = new JSONObject(content);
jsonObjects.getJSONArray("data").getJSONObject(0).getBoolean("valuesdata");
}
}
你可以考虑:
@Test
public void dropdownJsonValueIsStaticTrue() throws Exception {
String content = new String(Files.readAllBytes(Paths.get(input_file_path)));
JSONObject jsonObjects = new JSONObject(content);
JSONArray datas = jsonObjects.getJSONArray("data");
for (int i = 0 ; i < datas.length(); i++) {
JSONObject data = datas.getJSONObject(i);
boolean valuesdata = data.getBoolean("valuesdata");
if(valuesdata) {
assertTrue(data.getJSONArray("values") != null);
} else {
assertTrue(data.getString("sql") != null);
}
}
}