function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shiva ChandelShiva Chandel 

How to make a test case for api for with invalid argument ?

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:
@isTest
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); 
   
   // case 3 when the id is invalid 
        req.requestURI = '/services/apexrest/api/fetch_status/'+ 0125412548;
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;
        Account acct3 = RestDemo.getResult();
        system.assertEquals(acct3, null);
      
        
    }
}

The thing is test case 2 and 3 are not working.
AnudeepAnudeep (Salesforce Developers) 
I recommend printing the below value using system.debug to confirm if the URL is valid. Also, can you try making a GET call using the apex anonymous window or REST explorer in the workbench to confirm if the call returns 200 response?
 
req.requestURI = '/services/apexrest/api/fetch_status/' + id;

 
Shiva ChandelShiva Chandel
I have already checked on postman. It is working fine but not here Get Outlook for Android