如何基于唯一键验证各个值,使用Java进行Json响应验证测试
我有一个Json响应。在这里,我需要基于响应中的唯一键使用java进行验证测试。这里我想根据响应中的id进行验证。我将部分回复粘贴在这里供您参考。由于我是一名测试人员,对Json还不熟悉,如果有人能帮助我,我会非常感激 我使用下面的java代码来避免Json“{”错误如何基于唯一键验证各个值,使用Java进行Json响应验证测试,java,json,Java,Json,我有一个Json响应。在这里,我需要基于响应中的唯一键使用java进行验证测试。这里我想根据响应中的id进行验证。我将部分回复粘贴在这里供您参考。由于我是一名测试人员,对Json还不熟悉,如果有人能帮助我,我会非常感激 我使用下面的java代码来避免Json“{”错误 int a = entireResponse.indexOf("{"); entireResponse = entireResponse.substring(a); Json响应: [ { "id": 6160393
int a = entireResponse.indexOf("{");
entireResponse = entireResponse.substring(a);
Json响应:
[
{
"id": 6160393,
"name": "chef-haproxy",
"full_name": "sample/chef-haproxy",
"owner": {
"login": "sample",
"id": 155331,
"avatar_url": "https://avatars.githubusercontent.com/u/155331?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/sample",
"html_url": "https://github.com/sample",
"followers_url": "https://api.github.com/users/sample/followers",
"following_url": "https://api.github.com/users/sample/following{/other_user}",
"gists_url": "https://api.github.com/users/sample/gists{/gist_id}",
"starred_url": "https://api.github.com/users/sample/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/sample/subscriptions",
"organizations_url": "https://api.github.com/users/sample/orgs",
"repos_url": "https://api.github.com/users/sample/repos",
"events_url": "https://api.github.com/users/sample/events{/privacy}",
"received_events_url": "https://api.github.com/users/sample/received_events",
"type": "Organization",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/sample/chef-haproxy",
"description": "An attempt at a tested HaProxy Cookbook for Chef with lots of bells and whistles.",
"fork": true,
"url": "https://api.github.com/repos/sample/chef-haproxy",
"forks_url": "https://api.github.com/repos/sample/chef-haproxy/forks",
"keys_url": "https://api.github.com/repos/sample/chef-haproxy/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/sample/chef-haproxy/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/sample/chef-haproxy/teams",
"hooks_url": "https://api.github.com/repos/sample/chef-haproxy/hooks",
"issue_events_url": "https://api.github.com/repos/sample/chef-haproxy/issues/events{/number}",
"events_url": "https://api.github.com/repos/sample/chef-haproxy/events",
"assignees_url": "https://api.github.com/repos/sample/chef-haproxy/assignees{/user}",
"branches_url": "https://api.github.com/repos/sample/chef-haproxy/branches{/branch}",
"tags_url": "https://api.github.com/repos/sample/chef-haproxy/tags",
"blobs_url": "https://api.github.com/repos/sample/chef-haproxy/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/sample/chef-haproxy/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/sample/chef-haproxy/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/sample/chef-haproxy/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/sample/chef-haproxy/statuses/{sha}",
"languages_url": "https://api.github.com/repos/sample/chef-haproxy/languages",
"stargazers_url": "https://api.github.com/repos/sample/chef-haproxy/stargazers",
"contributors_url": "https://api.github.com/repos/sample/chef-haproxy/contributors",
"subscribers_url": "https://api.github.com/repos/sample/chef-haproxy/subscribers",
"subscription_url": "https://api.github.com/repos/sample/chef-haproxy/subscription",
"commits_url": "https://api.github.com/repos/sample/chef-haproxy/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/sample/chef-haproxy/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/sample/chef-haproxy/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/sample/chef-haproxy/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/sample/chef-haproxy/contents/{+path}",
"compare_url": "https://api.github.com/repos/sample/chef-haproxy/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/sample/chef-haproxy/merges",
"archive_url": "https://api.github.com/repos/sample/chef-haproxy/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/sample/chef-haproxy/downloads",
"issues_url": "https://api.github.com/repos/sample/chef-haproxy/issues{/number}",
"pulls_url": "https://api.github.com/repos/sample/chef-haproxy/pulls{/number}",
"milestones_url": "https://api.github.com/repos/sample/chef-haproxy/milestones{/number}",
"notifications_url": "https://api.github.com/repos/sample/chef-haproxy/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/sample/chef-haproxy/labels{/name}",
"releases_url": "https://api.github.com/repos/sample/chef-haproxy/releases{/id}",
"deployments_url": "https://api.github.com/repos/sample/chef-haproxy/deployments",
"created_at": "2012-10-10T16:26:35Z",
"updated_at": "2013-01-12T14:51:13Z",
"pushed_at": "2012-11-17T20:17:42Z",
"git_url": "git://github.com/sample/chef-haproxy.git",
"ssh_url": "git@github.com:sample/chef-haproxy.git",
"clone_url": "https://github.com/sample/chef-haproxy.git",
"svn_url": "https://github.com/sample/chef-haproxy",
"homepage": "",
"size": 110,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Ruby",
"has_issues": false,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"open_issues_count": 0,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "master"
},
{
"id": 5829904,
"name": "chef-protobuf",
"full_name": "sample/chef-protobuf",
"owner": {
"login": "sample",
"id": 155331,
"avatar_url": "https://avatars.githubusercontent.com/u/155331?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/sample",
"html_url": "https://github.com/sample",
"followers_url": "https://api.github.com/users/sample/followers",
"following_url": "https://api.github.com/users/sample/following{/other_user}",
"gists_url": "https://api.github.com/users/sample/gists{/gist_id}",
"starred_url": "https://api.github.com/users/sample/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/sample/subscriptions",
"organizations_url": "https://api.github.com/users/sample/orgs",
"repos_url": "https://api.github.com/users/sample/repos",
"events_url": "https://api.github.com/users/sample/events{/privacy}",
"received_events_url": "https://api.github.com/users/sample/received_events",
"type": "Organization",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/sample/chef-protobuf",
"description": "Protocol Buffers alfresco",
"fork": true,
"url": "https://api.github.com/repos/sample/chef-protobuf",
"forks_url": "https://api.github.com/repos/sample/chef-protobuf/forks",
"keys_url": "https://api.github.com/repos/sample/chef-protobuf/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/sample/chef-protobuf/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/sample/chef-protobuf/teams",
"hooks_url": "https://api.github.com/repos/sample/chef-protobuf/hooks",
"issue_events_url": "https://api.github.com/repos/sample/chef-protobuf/issues/events{/number}",
"events_url": "https://api.github.com/repos/sample/chef-protobuf/events",
"assignees_url": "https://api.github.com/repos/sample/chef-protobuf/assignees{/user}",
"branches_url": "https://api.github.com/repos/sample/chef-protobuf/branches{/branch}",
"tags_url": "https://api.github.com/repos/sample/chef-protobuf/tags",
"blobs_url": "https://api.github.com/repos/sample/chef-protobuf/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/sample/chef-protobuf/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/sample/chef-protobuf/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/sample/chef-protobuf/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/sample/chef-protobuf/statuses/{sha}",
"languages_url": "https://api.github.com/repos/sample/chef-protobuf/languages",
"stargazers_url": "https://api.github.com/repos/sample/chef-protobuf/stargazers",
"contributors_url": "https://api.github.com/repos/sample/chef-protobuf/contributors",
"subscribers_url": "https://api.github.com/repos/sample/chef-protobuf/subscribers",
"subscription_url": "https://api.github.com/repos/sample/chef-protobuf/subscription",
"commits_url": "https://api.github.com/repos/sample/chef-protobuf/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/sample/chef-protobuf/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/sample/chef-protobuf/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/sample/chef-protobuf/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/sample/chef-protobuf/contents/{+path}",
"compare_url": "https://api.github.com/repos/sample/chef-protobuf/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/sample/chef-protobuf/merges",
"archive_url": "https://api.github.com/repos/sample/chef-protobuf/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/sample/chef-protobuf/downloads",
"issues_url": "https://api.github.com/repos/sample/chef-protobuf/issues{/number}",
"pulls_url": "https://api.github.com/repos/sample/chef-protobuf/pulls{/number}",
"milestones_url": "https://api.github.com/repos/sample/chef-protobuf/milestones{/number}",
"notifications_url": "https://api.github.com/repos/sample/chef-protobuf/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/sample/chef-protobuf/labels{/name}",
"releases_url": "https://api.github.com/repos/sample/chef-protobuf/releases{/id}",
"deployments_url": "https://api.github.com/repos/sample/chef-protobuf/deployments",
"created_at": "2012-09-16T14:21:57Z",
"updated_at": "2014-10-12T15:02:14Z",
"pushed_at": "2014-10-12T15:02:13Z",
"git_url": "git://github.com/sample/chef-protobuf.git",
"ssh_url": "git@github.com:sample/chef-protobuf.git",
"clone_url": "https://github.com/sample/chef-protobuf.git",
"svn_url": "https://github.com/sample/chef-protobuf",
"homepage": null,
"size": 98,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Ruby",
"has_issues": false,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"open_issues_count": 0,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "master"
},
{
"id": 11471217,
"name": "django-filer",
"full_name": "sample/django-filer",
"owner": {
"login": "sample",
"id": 155331,
"avatar_url": "https://avatars.githubusercontent.com/u/155331?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/sample",
"html_url": "https://github.com/sample",
"followers_url": "https://api.github.com/users/sample/followers",
"following_url": "https://api.github.com/users/sample/following{/other_user}",
"gists_url": "https://api.github.com/users/sample/gists{/gist_id}",
"starred_url": "https://api.github.com/users/sample/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/sample/subscriptions",
"organizations_url": "https://api.github.com/users/sample/orgs",
"repos_url": "https://api.github.com/users/sample/repos",
"events_url": "https://api.github.com/users/sample/events{/privacy}",
"received_events_url": "https://api.github.com/users/sample/received_events",
"type": "Organization",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/sample/django-filer",
"description": "File and Image Management Application for django",
"fork": true,
"url": "https://api.github.com/repos/sample/django-filer",
"forks_url": "https://api.github.com/repos/sample/django-filer/forks",
"keys_url": "https://api.github.com/repos/sample/django-filer/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/sample/django-filer/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/sample/django-filer/teams",
"hooks_url": "https://api.github.com/repos/sample/django-filer/hooks",
"issue_events_url": "https://api.github.com/repos/sample/django-filer/issues/events{/number}",
"events_url": "https://api.github.com/repos/sample/django-filer/events",
"assignees_url": "https://api.github.com/repos/sample/django-filer/assignees{/user}",
"branches_url": "https://api.github.com/repos/sample/django-filer/branches{/branch}",
"tags_url": "https://api.github.com/repos/sample/django-filer/tags",
"blobs_url": "https://api.github.com/repos/sample/django-filer/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/sample/django-filer/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/sample/django-filer/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/sample/django-filer/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/sample/django-filer/statuses/{sha}",
"languages_url": "https://api.github.com/repos/sample/django-filer/languages",
"stargazers_url": "https://api.github.com/repos/sample/django-filer/stargazers",
"contributors_url": "https://api.github.com/repos/sample/django-filer/contributors",
"subscribers_url": "https://api.github.com/repos/sample/django-filer/subscribers",
"subscription_url": "https://api.github.com/repos/sample/django-filer/subscription",
"commits_url": "https://api.github.com/repos/sample/django-filer/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/sample/django-filer/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/sample/django-filer/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/sample/django-filer/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/sample/django-filer/contents/{+path}",
"compare_url": "https://api.github.com/repos/sample/django-filer/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/sample/django-filer/merges",
"archive_url": "https://api.github.com/repos/sample/django-filer/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/sample/django-filer/downloads",
"issues_url": "https://api.github.com/repos/sample/django-filer/issues{/number}",
"pulls_url": "https://api.github.com/repos/sample/django-filer/pulls{/number}",
"milestones_url": "https://api.github.com/repos/sample/django-filer/milestones{/number}",
"notifications_url": "https://api.github.com/repos/sample/django-filer/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/sample/django-filer/labels{/name}",
"releases_url": "https://api.github.com/repos/sample/django-filer/releases{/id}",
"deployments_url": "https://api.github.com/repos/sample/django-filer/deployments",
"created_at": "2013-07-17T08:34:43Z",
"updated_at": "2015-12-02T10:53:10Z",
"pushed_at": "2013-07-17T09:07:47Z",
"git_url": "git://github.com/sample/django-filer.git",
"ssh_url": "git@github.com:sample/django-filer.git",
"clone_url": "https://github.com/sample/django-filer.git",
"svn_url": "https://github.com/sample/django-filer",
"homepage": "https://github.com/stefanfoulis/django-filer/wiki",
"size": 3431,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Python",
"has_issues": false,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"open_issues_count": 0,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "develop"
},
{
"id": 21089256,
如果您使用JSON库来进行验证,那么将更容易、更可靠 这将帮助您设置库 设置完成后,请查看您的Json数据 数据结构如下
JSONParser js = new JSONParser();
Object obj = js.parse(entireResponse);
JSONArray data = (JSONArray) obj;
for(int i=0;i<data.size();i++){
JSONObject response = (JSONObject)data.get(i);
Long id = (Long) response.get("id");
String name = (String) response.get("name");
String full_name = (String) response.get("full_name");
JSONObject owner = (JSONObject) response.get("owner");
String owner_login = (String) owner.get("login");
System.out.println("ID :"+id);
System.out.println("Name :"+name);
System.out.println("Full Name :"+full_name);
System.out.println("Owner Login :"+owner_login);
}
JSONParser js=new JSONParser();
objectobj=js.parse(entireResponse);
JSONArray数据=(JSONArray)obj;
对于(inti=0;i而言,如果使用JSON库进行验证,将更容易、更可靠
这将帮助您设置库
设置完成后,请查看您的Json数据
数据结构如下
这是一系列反应
每个响应都有id、name、fullname、owner和一组其他键值对
所有者进一步指出更多属性,如登录名、id等
下面的代码显示了如何提取每个键值对
JSONParser js = new JSONParser();
Object obj = js.parse(entireResponse);
JSONArray data = (JSONArray) obj;
for(int i=0;i<data.size();i++){
JSONObject response = (JSONObject)data.get(i);
Long id = (Long) response.get("id");
String name = (String) response.get("name");
String full_name = (String) response.get("full_name");
JSONObject owner = (JSONObject) response.get("owner");
String owner_login = (String) owner.get("login");
System.out.println("ID :"+id);
System.out.println("Name :"+name);
System.out.println("Full Name :"+full_name);
System.out.println("Owner Login :"+owner_login);
}
JSONParser js=new JSONParser();
objectobj=js.parse(entireResponse);
JSONArray数据=(JSONArray)obj;
对于(int i=0;i,测试库允许您通过将JSON文档与同样作为JSON提供的模式进行匹配来验证JSON文档的结构和/或值
假设您只需要验证一些结构方面或值,如:
- JSON的根(
$
)是一个数组
- 数组在第一个位置包含一个对象(
$[0]
),该对象至少具有属性“id”
、“名称”
、“全名”
和“所有者”
,并且可能包含零个或多个其他属性
- 您希望
“id”
属性等于6160393
,“name”
属性等于“chef haproxy”
,但您不关心“full\u name”属性的值
- 属性
“$[0]。所有者”
是一个至少具有“id”
属性且可能具有其他属性的对象
- 位置
$
处的数组可能包含其他对象
然后可以使用以下JSON作为模式:
[
{
"id": 6160393,
"name": "chef-haproxy",
"full_name": "...",
"owner": {
"id": 155331,
"...": "..."
},
"...": "..."
},
"..."
]
JUnit测试中的用法:
@Test
public void test() throws Exception {
String actual = <<read your JSON file as string>>;
String pattern = <<read the JSON pattern file as string>>;
Majx.assertJsonMatches(pattern, actual);
}
@测试
public void test()引发异常{
字符串实际值=;
字符串模式=;
Majx.assertJsonMatches(模式、实际);
}
错误消息将告诉您JSON与您的期望不符的地方。测试库允许您通过将JSON文档的结构和/或值与同样作为JSON提供的模式进行匹配来验证JSON文档的结构和/或值
假设您只需要验证一些结构方面或值,如:
- JSON的根(
$
)是一个数组
- 数组在第一个位置包含一个对象(
$[0]
),该对象至少具有属性“id”
、“名称”
、“全名”
和“所有者”
,并且可能包含零个或多个其他属性
- 您希望
“id”
属性等于6160393
,“name”
属性等于“chef haproxy”
,但您不关心“full\u name”属性的值
- 属性
“$[0]。所有者”
是一个至少具有“id”
属性且可能具有其他属性的对象
- 位置
$
处的数组可能包含其他对象
然后可以使用以下JSON作为模式:
[
{
"id": 6160393,
"name": "chef-haproxy",
"full_name": "...",
"owner": {
"id": 155331,
"...": "..."
},
"...": "..."
},
"..."
]
JUnit测试中的用法:
@Test
public void test() throws Exception {
String actual = <<read your JSON file as string>>;
String pattern = <<read the JSON pattern file as string>>;
Majx.assertJsonMatches(pattern, actual);
}
@测试
public void test()引发异常{
字符串实际值=;
字符串模式=;
Majx.assertJsonMatches(模式、实际);
}
一条错误消息将告诉您JSON与您的期望不符的地方