0
votes

I have created a trigger which calls a future class to make http callout to a third party url, everthing is working fine here but the test class is not covering the opportunity fields IsWon & IsClosed. What modification do I need in test class to make the code coverage at least 75 % for this trigger.

// Apex trigger

trigger oppTrigger on Opportunity (before update) {

String oppType = '';
for(Opportunity opp : Trigger.new){

if (opp.IsClosed == true){  // closed
  if (opp.IsWon == true){
    oppType = 'Won'; // closed-won
   }else{
    oppType = 'Lost'; // closed-lost
   }
} else { // open
       oppType = 'Open'; 
    }
  // call a method with @future annotation
   futureCls.srvcCallout(opp.id,opp.Amount,oppType); 
 }
}

// future class for trigger with future method

global class futureCls {
@future(callout=true)
Public static void srvcCallout(String oppId, Decimal oppAmt, String oppType){

     // Create http request
     HttpRequest req = new HttpRequest();
     req.setMethod('POST');
     req.setHeader('Content-Type', 'application/json;charset=UTF-8');     
     req.setEndpoint('https://www.testurl.com/salesforce/opp-change'+'?id='+oppId+'&amt='+oppAmt+'&stage='+oppType);

     // create web service
     Http http = new Http();
      try {
        // Execute web service call here     
        HTTPResponse res = http.send(req);  
        // Debug messages
        System.debug('RESPONSE:'+res.toString());
        System.debug('STATUS:'+res.getStatus());
        System.debug('STATUS_CODE:'+res.getStatusCode());
        System.debug('BODY:'+res.getBody());

        } catch(System.CalloutException e) {
             // Exception handler
             System.debug('Error connecting to Paperless..');
       }   
     }
 }

// Test class for the trigger where I'm stuck:-

@isTest
private class futureCls_Test {  

 private static testMethod void srvcCallout_Test() {        

    Test.startTest();

    // Unit test to cover trigger update event
    Opportunity opp = new Opportunity(Name='test opp', StageName='stage', Probability = 95, CloseDate=system.today());
    insert opp;
    opp.Amount = 1000;
    opp.StageName = 'Closed/Won';
    update opp;

    // Assign some test values
    String oppId = '1sf2sfs2';
    Decimal oppAmt = 4433.43;
    String oppType = 'Won';

    // Unit test to cover future method
    futureCls.srvcCallout(oppId, oppAmt,oppType);    

    // Unit test to cover http web service
    Test.setMock(HttpCalloutMock.class, new futureClsCalloutMock()); 
    Test.stopTest();

  }
}
1

1 Answers

1
votes

Your test class will have to do the following to hit all of that trigger:

Note, this is just one way to do it, you could do it a few different ways

  • Create a new opportunity
  • Update the opportunity to some status that's "Open"
  • Create a new opportunity
  • Update the opportunity to Closed/Lost
  • Create a new opportunity
  • Update the opportunity to Closed/Won

If you asked me, a TestDataFactory function that created an opportunity and then updated it to a specified status would be helpful:

@isTest
public testOpportunityWithStatusChange(targetStatus){
    //do stuff here
};

You could then call that factory once for every status you wanted to check within your test class to cover the trigger.