0

Class :

@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;
    }
}

Test Class :

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); 
        
    }
}

I want to test the test case 2 where the account with the random id is not present. But while testing the test class (in test case 2 )it is giving exception.

In this i used an existing id and changed it a little.So while test case2 runs it should go through the else statement of the main class but it is throwing exception (catch statement,Tested it in salesforce developer console).

1 Answers1

1

account = [SELECT Id, Name FROM Account WHERE Id = :accountId Limit 1];

The way you written it account will never be null. It'll just throw "List has no rows for assignment to SObject".

Change to this

List<Account> accs = [SELECT Id, Name FROM Account WHERE Id = :accountId Limit 1];
if(!accs.isEmpty()){
   // return 200;
} else {
   // return 400;
}

If you query that way it'll always come as List. It can be empty list but it will be a list (not null). Assign query results to single Account/Contact/... only if you're 100% sure it'll return something. Or catch the exceptions

eyescream
  • 18,088
  • 2
  • 34
  • 46