+ Start a Discussion
trailhead solutions 10trailhead solutions 10 

Test Class Code Coverage for Rest API Http Patch

Hello All
The test class for the below Apex Rest API with Http Patch Method is failing and at 0% Code Coverage. Can someone please help to complete this in order to deploy to Production.

Apex Class:
@RestResource(urlMapping='/opportunityiWiseUpdate/*')
global class opportunityiWiseUpdate {
    public class responseWrapper{
      public id SalesForceId;
      public Date EffectiveDate;
      public string stage;
      //public date closedate;
      public Decimal ContractValue;
      }

    @httppatch
      global Static string fetchopportunity(){
      RestRequest req = RestContext.request;
      RestResponse res = Restcontext.response;
          system.debug('req : ' + req);
      string jsonString=req.requestBody.tostring();
          system.debug(jsonString);
      responseWrapper wResp=(responseWrapper) JSON.deserialize(jsonString,responseWrapper.class);
          system.debug('wResp : ' + wResp);
         
          for(opportunity op : [select id,recordtype.name,stagename,closedate,Amount,TCV__c from opportunity 
                                where id =:wResp.SalesForceId ]){
          system.debug('op : ' + op);
         // if(op.id !=null){
              if(op.recordtype.Name=='HOS'){
               op.Id = wResp.SalesForceId;
               op.Amount = wResp.ContractValue;
               op.stagename = wResp.stage;
               op.closedate = wResp.EffectiveDate;
              update op;
          }
               else if(op.recordtype.Name=='IBS'){
               op.Id = wResp.SalesForceId;
               op.TCV__C = wResp.ContractValue;
               op.stagename = wResp.stage;
               op.closedate = wResp.EffectiveDate;
              update op;
          }
          }
             
          return 'sucess';
      }
       
}

Test Class:
@isTest
global class opportunityiWiseUpdateTest {
   @testsetup
    static void datasetup(){ 
       
        String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'Account' and Name = 'IBS'].Id;
    Account a = new Account();
            a.Name = 'EXPEDIA';
            a.Segment__c = 'Aviation';
            a.Sub_Segment__c = 'Startup';
            a.IBS_Region__c = 'IBSZ';
            a.IBS_Sub_Region__c = 'ANZ';
            a.Account_Domain_Name__c = 'test.com';
            a.Account_Code__c='MIII';
            a.Approved__c = True;
            a.RecordTypeId=strRecordTypeId;
            insert a;
    
    String strRecordTypeId1 = [Select Id From RecordType Where SobjectType = 'Opportunity' and Name = 'IBS'].Id;
           Opportunity o = new Opportunity();
                
            o.Name = 'Test Opportunity ibs';
            o.AccountId = a.Id;
           // o.Opportunity_Name__c = 'Test Record';
            o.Contract_Type__c = 'CDx Services';
            o.TCV__c = 5000;
            o.CloseDate = system.today();
            o.Type_of_Deal__c = 'CR';
            o.Opportunity_Source__c = 'Other';
                o.Type_of_Proposal__c='RFP';
            o.StageName = 'Closed Won';
           o.IBS_Lob__c = 'Adopt - ADOPT';
                o.Reason_for_Closed_Won__c='Pricing';
           o.Products__c = 'iFlight Crew Planning Classic';
            o.Incumbent__c = 'Sabre';
           o.Tenure__c = 6;
            o.Tenure_Unit_of_Measure__c = 'Years';
           o.One_Time_Fee__c = 5000;
            o.Reason_for_Winning_or_Losing__c ='Pricing';
            o.Annual_Recurring_Revenue__c = 100001;
            o.RecordTypeId=strRecordTypeId1;
            insert o;
            
     
                
    String strRecordTypeIda1 = [Select Id From RecordType Where SobjectType = 'Account' and Name = 'HOS'].Id;
    Account a1 = new Account();
            a1.Name = 'EXPEDIA1';
            a1.Type='Prospect';
            a1.Industry='Supply';
            a1.RecordTypeId=strRecordTypeIda1;
            insert a1;
    
    String strRecordTypeIdo1 = [Select Id From RecordType Where SobjectType = 'Opportunity' and Name = 'HOS'].Id;
           Opportunity o1 = new Opportunity();
                
            o1.Name = 'Test Opportunity';
            o1.AccountId = a1.Id;
           // o1.Opportunity_Name__c = 'Test Record';
            o1.StageName = 'Closed Won';
            o1.Category__c='CORE';
            o1.Type='Supply - Accom';
            o1.CloseDate = system.today();
            o1.Amount=8520;
            o1.Term__c=5;
            o1.Implementation_Fee__c=58;
            o1.Ramp_to_Live__c=5;
            o1.Ramp_to_Maturity__c=6;
            o1.Probability=10;
            o1.Finance_TCV_Stage__c='Initial - 10%';
            o1.Lead_Generation_Date__c=system.today();
            o1.RecordTypeId=strRecordTypeIdo1;
            insert o1;
    }
    
       @isTest
   static void updateBOClassMethod(){
   Test.startTest();
 Opportunity sHOppRec= [SELECT Id,Name,recordtype.DeveloperName,StageName,CloseDate,Amount From Opportunity Where Name ='Test Opportunity' Limit 1];
system.debug('sHOppRec : ' + sHOppRec);        
   SingleRequestMock fakeResponse1 = new SingleRequestMock(sHOppRec.Id,22500,
                                                
                                                 'Contracted',
                                                 system.today()+1
                                                   );
    String JsonMsg=JSON.serialize(fakeResponse1);
        system.debug('JsonMsg : ' + JsonMsg);
   RestRequest req = new RestRequest(); 
   RestResponse res = new RestResponse();

    req.requestURI = '/services/apexrest/DemoUrl';  //Request URL
    req.httpMethod = 'PATCH';//HTTP Request Type
    req.requestBody = Blob.valueof(JsonMsg);
    RestContext.request = req;
    RestContext.response= res; 
   opportunityiWiseUpdate.fetchopportunity();
    Test.stopTest();
}
    
    
    
 @isTest
    static void updateBOClassMethod1(){
   Test.startTest();
 Opportunity sIOppRec= [SELECT Id,Name,recordtype.Name,StageName,CloseDate,TCV__c From Opportunity Where Name ='Test Opportunity ibs' Limit 1];
        system.debug('sIOppRec : ' + sIOppRec);
   SingleRequestMock fakeResponse1 = new SingleRequestMock(sIOppRec.Id,82503,
                                               
                                                 'Contracted',
                                                 system.today()+2
                                                   );
    String JsonMsg=JSON.serialize(fakeResponse1);
        system.debug('JsonMsg : ' + JsonMsg);
   RestRequest req = new RestRequest(); 
   RestResponse res = new RestResponse();

    req.requestURI = '/services/apexrest/DemoUrl';  //Request URL
    req.httpMethod = 'PUT';//HTTP Request Type
    req.requestBody = Blob.valueof(JsonMsg);
    RestContext.request = req;
    RestContext.response= res;
   opportunityiWiseUpdate.fetchopportunity();
    Test.stopTest();
}
 public class SingleRequestMock {
        
      public id SalesForceId;
      public Date EffectiveDate;
      public string stage;
     // public date closedate;
      public Decimal ContractValue;
        
            public SingleRequestMock(id SalesForceId, Decimal ContractValue, string stage,Date EffectiveDate) {
            this.SalesForceId = SalesForceId;
            this.EffectiveDate = EffectiveDate;
            this.stage = stage;
            this.ContractValue = ContractValue;
        }
    }
  
}

Thanks in Advance!
AbhinavAbhinav (Salesforce Developers) 
Hi,

Please  also share the error with community. Why is it failing?

Thanks