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
sfg1sfg1 

REST API code coverage

I have REST API class, i am trying to cover 100% code coverage. Currently i have 71 % code coverage. Code in bold are not covered. Can you please guide me.

Apex class:
@RestResource(urlMapping='/mrtValidateNonFSLUser/*')

// This rest service is used by Web-application in order to
// MRT user who are non-FSL.
// Retrieves username and password as post validates against "MRT__c" Table  
// Sample Data
// {
//   "sUsername": "me@4richie.com",
//   "sPassword": "Epirc1961"
//}
global class mrtValidateNonFSLUser {
    //Input Class
    public class mrtCredintials{
        public String sUsername;
        public String sPassword;
    }    
    
    @HttpPost
    global static Boolean mrtValidateNonFSLUser () {
        //Variables declaration.        
        List<MRT__c> lstCKSWUser;
        mrtCredintials objCred;        
        RestRequest objReq = RestContext.request;
        RestResponse objRes = RestContext.response;   
        objRes.addHeader('Content-Type','applicatin/json');
        
        try{            
            objCred  = (mrtCredintials) JSON.deserialize(objReq.requestBody.toString(), mrtCredintials.class);
        }
        Catch(Exception objExp){
           return false; //Logging and possible alert to tech team along with exception message.???
        }
         if(objCred != NULL){
            lstCKSWUser = [SELECT Email__c From MRT__c WHERE Email__c =: objCred.sUsername AND Password__c =: objCred.sPassword AND Active__c = true];
            return lstCKSWUser.size() > 0 ? true : false;
            }
        return false;
    }       
}


TEST class:
@IsTest
public class Test_mrtValidateNonFSLUser {
     static testMethod void Method1() {
      MRT__c mrt1 = new MRT__c(
      Name = 'MRT One',
      City__c = 'city', 
      State__c = 'cp', 
      Zipcode__c = '123456',
      MRTLocation__Latitude__s = 33.9153,
      MRTLocation__Longitude__s =-118.351309,
               Email__c = 'test@epi.com',
               password__c = 'test1234'
      );
    insert mrt1; 
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        req.requestURI = '/services/apexrest/mrtValidateNonFSLUser';  
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;
       test.startTest();
         mrtValidateNonFSLUser.mrtValidateNonFSLUser();
          test.stopTest();
     }
      }
    


 
Aslam ChaudharyAslam Chaudhary
Use below code
If( Test.isRunningTest())
	
	objCred  = ‘’;//Pass your correct JSON
Else

objCred  = (mrtCredintials) JSON.deserialize(objReq.requestBody.toString(), mrtCredintials.class);