如何在salesforce中将api请求从测试类发送到主类?
类别:如何在salesforce中将api请求从测试类发送到主类?,api,salesforce,integration-testing,apex,Api,Salesforce,Integration Testing,Apex,类别: @RestResource(urlMapping='/api/fetch_status/*') global class RestDemo{ @HttpGet global static Account getResult(){ Account account; String accountId = ''; RestRequest restReq = RestContext.request;
@RestResource(urlMapping='/api/fetch_status/*')
global class RestDemo{
@HttpGet
global static Account getResult(){
Account account;
String accountId = '';
RestRequest restReq = RestContext.request;
RestResponse restRes = RestContext.response;
// reading url
try{
//accountId = restReq.params.get('accountId');
accountId = restReq.requestURI.substring(restReq.requestURI.lastIndexOf('/') + 1);
account = [SELECT Id, Name FROM Account WHERE Id = :accountId Limit 1];
if(account != null){ //checked whether any record is returned or not
restRes.responseBody = Blob.valueOf(JSON.serialize(account));
restRes.statusCode = 200;
}else{
/* String account_not_found= '{"message":"Not Found"}';
restRes.responseBody = Blob.valueOf(account_not_found); */
restRes.statusCode = 404;
}
}
catch(Exception ex){
restRes.responseBody = Blob.valueOf(ex.getMessage());
restRes.statusCode = 500;
}
return account;
}
}
private class RestDemoTest {
@testSetup
static void dataSetup() {
Account acc = new Account(Name = 'Testing5');
insert acc;
}
static testMethod void testGet() {
//case 1 when the id is valid
Account acc = [ SELECT Id FROM Account LIMIT 1 ];
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/api/fetch_status/' + acc.Id;
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response= res;
Account acct1 = RestDemo.getResult();
system.assertEquals(acct1.Name, 'Testing5');
// case 2 when the id is not present
String str_id = '0012x000004UjZX';
Id id = Id.valueOf(str_id);
req.requestURI = '/services/apexrest/api/fetch_status/' + id;
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response= res;
Account acct2 = RestDemo.getResult();
system.assertEquals(acct2, null);
}
}
测试类:
@RestResource(urlMapping='/api/fetch_status/*')
global class RestDemo{
@HttpGet
global static Account getResult(){
Account account;
String accountId = '';
RestRequest restReq = RestContext.request;
RestResponse restRes = RestContext.response;
// reading url
try{
//accountId = restReq.params.get('accountId');
accountId = restReq.requestURI.substring(restReq.requestURI.lastIndexOf('/') + 1);
account = [SELECT Id, Name FROM Account WHERE Id = :accountId Limit 1];
if(account != null){ //checked whether any record is returned or not
restRes.responseBody = Blob.valueOf(JSON.serialize(account));
restRes.statusCode = 200;
}else{
/* String account_not_found= '{"message":"Not Found"}';
restRes.responseBody = Blob.valueOf(account_not_found); */
restRes.statusCode = 404;
}
}
catch(Exception ex){
restRes.responseBody = Blob.valueOf(ex.getMessage());
restRes.statusCode = 500;
}
return account;
}
}
private class RestDemoTest {
@testSetup
static void dataSetup() {
Account acc = new Account(Name = 'Testing5');
insert acc;
}
static testMethod void testGet() {
//case 1 when the id is valid
Account acc = [ SELECT Id FROM Account LIMIT 1 ];
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/api/fetch_status/' + acc.Id;
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response= res;
Account acct1 = RestDemo.getResult();
system.assertEquals(acct1.Name, 'Testing5');
// case 2 when the id is not present
String str_id = '0012x000004UjZX';
Id id = Id.valueOf(str_id);
req.requestURI = '/services/apexrest/api/fetch_status/' + id;
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response= res;
Account acct2 = RestDemo.getResult();
system.assertEquals(acct2, null);
}
}
我想测试测试用例2,其中不存在具有随机id的帐户。
但是在测试测试类(在测试用例2中)时,它给出了异常。
在本文中,我使用了一个现有的id,并对其进行了一些更改。因此,当测试用例2运行时,它应该通过主类的else语句,但它引发了异常(catch语句,在salesforce developer控制台中对其进行了测试)。
account=[选择id,account中的名称,其中id=:accountId Limit 1]代码>
您编写帐户的方式永远不会为空。它只会抛出“列表中没有要分配给SObject的行”
换成这个
List<Account> accs = [SELECT Id, Name FROM Account WHERE Id = :accountId Limit 1];
if(!accs.isEmpty()){
// return 200;
} else {
// return 400;
}
List accs=[从Id=:accountId Limit 1]的帐户中选择Id、名称];
如果(!accs.isEmpty()){
//返回200;
}否则{
//返回400;
}
如果你这样查询,它总是以列表的形式出现。它可以是空列表,但它将是一个列表(非空)。将查询结果分配给单个帐户/联系人/。。。除非你百分之百确定它会有回报。或者抓住例外