salesforce - How to write test class for apex trigger on opportunity -


i have created trigger calls future class make http callout third party url, everthing working fine here test class not covering opportunity fields iswon & isclosed. modification need in test class make code coverage @ least 75 % 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 method @future annotation    futurecls.srvccallout(opp.id,opp.amount,opptype);   } } 

// future class trigger 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 paperless..');        }         }  } 

// test class trigger i'm stuck:-

@istest private class futurecls_test {     private static testmethod void srvccallout_test() {              test.starttest();      // unit test 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 test values     string oppid = '1sf2sfs2';     decimal oppamt = 4433.43;     string opptype = 'won';      // unit test cover future method     futurecls.srvccallout(oppid, oppamt,opptype);          // unit test cover http web service     test.setmock(httpcalloutmock.class, new futureclscalloutmock());      test.stoptest();    } } 

your test class have following hit of trigger:

note, 1 way it, few different ways

  • create new opportunity
  • update opportunity status that's "open"
  • create new opportunity
  • update opportunity closed/lost
  • create new opportunity
  • update opportunity closed/won

if asked me, testdatafactory function created opportunity , updated specified status helpful:

@istest public testopportunitywithstatuschange(targetstatus){     //do stuff here }; 

you call factory once every status wanted check within test class cover trigger.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -