Skip to main content
Prashanth Manivannan (lti) ha fatto una domanda in #Apex
Hi guys request you to help me to cover test class for the following code. When am tried to cover the test class it shows error attempt to dereference a null object. please help me out am new to test class development tried multiple ways...please help me out

@RestResource(urlmapping='/updateEngagement/*')

global class CPUpdateEngagement{

    @HttpPost

    global static Boolean updateEngagement(){

        try{

            RestRequest req = RestContext.request;

            if((req != NULL) && (req.requestBody != NULL)){

                RequestWrapper request = (RequestWrapper) JSON.deserialize(req.requestBody.toString(), RequestWrapper.Class);

                if(request.agreementId != NULL){

                    Apttus__APTS_Agreement__c agrRec = new Apttus__APTS_Agreement__c(Id = request.agreementId, Changepoint_Engagement_ID__c = request.inputString);

                    update agrRec;

                    

                }

                if(request.opportunityId != NULL){

                    opportunity oppRec = new opportunity(Id = request.opportunityId, Engagement_Id__c = request.inputString);

                    update oppRec;

                    

                }

                return true;

            }

            return false;

        } catch(Exception ex){

            return false;

        }

    }

    

    global class RequestWrapper{

     public Id agreementId;

     public Id opportunityId;

      String inputString;

    }

}

my test class 

@isTest(SeeAllData = true)

public class sampleTest{

 static testMethod void testMethod2(){

               

              Agreement__c  AAA  = new  Agreement__c();

               

                

            AAA .RecordTypeId = Schema.SObjectType.Apttus__APTS_Agreement__c.getRecordTypeInfosByName().get('SOW-EMEA').getRecordTypeId();

            AAA .Status__c ='Other Party Signatures';

            AAA .Status_Category__c = 'In Signatures';

            AAA .geo__c='APAC';

            AAA .Approval_Required__c = true;

            AAA .Territories__c = 'United States;India';

            

            

            opportunity opp = new opportunity();

            opp.RecordTypeId = Schema.SObjectType.opportunity.getRecordTypeInfosByName().get('Managed Sales Opportunity').getRecordTypeId();

            opp.name = 'test';

            opp.StageName = 'Budget Approved';

            

            CPUpdateEngagement.RequestWrapper reqst = new CPUpdateEngagement.RequestWrapper();

            reqst.agreementId = AAA.id;

            reqst.opportunityId = opp.id;

            

            Test.startTest();

            RestRequest req = new RestRequest(); 

            req = RestContext.request;

           // RequestWrapper request = (RequestWrapper) JSON.deserialize(req.requestBody.toString(), RequestWrapper.Class);

           

           // String JsonMsg=req.requestBody.toString();

           

            req.requestURI = '/updateEngagement/*';  //Request URL

            req.httpMethod = 'POST';//HTTP Request Type

         //   req.requestBody = Blob.valueof(JsonMsg);

           

           CPUpdateEngagement resp =new CPUpdateEngagement();

            update AAA;

            update opp;

            }

           }
1 risposta
0/9000