You need to sign in to do that
Don't have an account?
Javwad Azeem
Test class for @future HTTP callout method
Hey Team,
I need help to write a test class for the below code. I have checked developer forums stackexhnage but havent't find anything appropriate to get the code coverage.
I tried creating Mock class, calling method between Test.startTest() and Test.stopTest and even tried exception handling to catch the exception.
Booh, Stuck Now!!!
I would really appreciate if anyone can help me to write test class for the below code.
Thanks & Regards,
Javwad Azeem
I need help to write a test class for the below code. I have checked developer forums stackexhnage but havent't find anything appropriate to get the code coverage.
I tried creating Mock class, calling method between Test.startTest() and Test.stopTest and even tried exception handling to catch the exception.
Booh, Stuck Now!!!
I would really appreciate if anyone can help me to write test class for the below code.
@future(callout = true) public static void getResultTestRun() { Integer intTotalPercentageCovered = 0; String objectIdQuery = 'SELECT PercentCovered FROM ApexOrgWideCoverage'; String environmentURL = URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v37.0/tooling/query/?q=' + EncodingUtil.urlEncode(objectIdQuery, 'UTF-8'); String username = 'shasahu@hcl.com'; String password = 'Myhcl12#'; partnerSoapSforceCom.Soap sp = new partnerSoapSforceCom.Soap(); partnerSoapSforceCom.LoginResult loginResult = sp.login(username, password); //sessionId = loginResult.sessionId; //system.debug('sessionId: '+sessionId); HttpRequest req = new HttpRequest(); req.setHeader('Authorization', 'Bearer ' + loginResult.sessionId); req.setHeader('Content-Type', 'application/json'); req.setEndpoint(environmentURL); req.setMethod('GET'); Http h = new Http(); HttpResponse res = h.send(req); system.debug('==1==' + res.getBody()); for(JSON2Apex2.Records objRec : JSON2Apex2.parse(res.getBody()).records) { System.debug(objRec.PercentCovered ); if(objRec.PercentCovered != null) intTotalPercentageCovered = objRec.PercentCovered; else intTotalPercentageCovered = 0; } List<ApexTestResult> lstART = [select ApexClassId , MethodName , Id, ApexClass.Name ,Outcome, Message from ApexTestResult]; string header = 'Class Name, MethodName, Message \n'; string finalstr = header ; for(ApexTestResult a: lstART) { if(a.Outcome != 'Pass') { string recordString = a.ApexClass.Name+','+a.MethodName+','+a.Message.normalizeSpace()+'\n'; finalstr = finalstr +recordString; } } // Callout #2 HttpRequest req2 = new HttpRequest(); req2.setHeader('Authorization', 'Bearer ' + loginResult.sessionId); req2.setHeader('Content-Type', 'application/json'); req2.setEndpoint(URL.getSalesforceBaseURL().toExternalForm()+'/services/data/v33.0/tooling/query/?q=Select+id,ApexClassorTrigger.Name,NumLinesCovered,NumLinesUncovered+from+ApexCodeCoverageAggregate'); req2.setMethod('GET'); Http h2 = new Http(); HttpResponse res2 = h2.send(req2); System.debug('response: '+res2.getBody()); Map<String, Integer > clsVsCov = new Map<String, Integer>(); Integer totalCovered = 0; Integer totalUnCovered = 0; for(JSON2Apex.Records objRec : JSON2Apex.parse(res2.getBody()).records) { System.debug(objRec.ApexClassOrTrigger.Name); System.debug(objRec.ApexClassOrTrigger.attributes); if(objRec.NumLinesCovered != null && objRec.NumLinesUncovered != null) { System.debug('AAAA: '+objRec.NumLinesCovered + objRec.NumLinesUncovered ); if(objRec.NumLinesCovered + objRec.NumLinesUncovered != 0 ) { totalCovered = totalCovered+objRec.NumLinesCovered; totalUnCovered = totalUnCovered+objRec.NumLinesUncovered; Double totalNOLines = objRec.NumLinesCovered + objRec.NumLinesUncovered; System.debug('Total Number of Lines: '+totalNOLines); System.debug('objRec.NumLinesCovered: '+objRec.NumLinesCovered); Double coveredPercent = (objRec.NumLinesCovered/totalNOLines)*100; System.debug('coveredPercent: '+coveredPercent); clsVsCov.put(objRec.ApexClassOrTrigger.Name , integer.valueOf(Math.round(coveredPercent))); } else clsVsCov.put(objRec.ApexClassOrTrigger.Name , 0); } } Integer totalCovered2 = 0; Integer totalClass = 0; String header1 = 'Classe Name , Percentage Covered \n'; String finalstr1 = header1 ; for(String strKey : clsVsCov.keySet()) { string recordString = strKey+','+clsVsCov.get(strKey)+'\n'; finalstr1 = finalstr1 +recordString; } Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment(); blob csvBlob = Blob.valueOf(finalstr); string csvname= 'Failed Classes Status.csv'; csvAttc.setFileName(csvname); csvAttc.setBody(csvBlob); Messaging.EmailFileAttachment csvAttc1 = new Messaging.EmailFileAttachment(); blob csvBlob1 = Blob.valueOf(finalstr1); string csvname1= 'Pass Classes Status.csv'; csvAttc1.setFileName(csvname1); csvAttc1.setBody(csvBlob1); List<Run_Test__c> RunTestCS = Run_Test__c.getall().values(); System.debug(RunTestCS[0].Org_Name__c); String orgName = RunTestCS[0].Org_Name__c; String email = RunTestCS[0].Recipient__c; Messaging.SingleEmailMessage email1 =new Messaging.SingleEmailMessage(); String[] toAddresses1 = new list<string> {email}; String subject1 ='Code Coverage Result : ' + Datetime.now().format('dd MMMM YYYY'); email1.setSubject(subject1); email1.setToAddresses(toAddresses1); //email1.setPlainTextBody('Total Percentage covered of org is : ' + intTotalPercentageCovered); String messageBody='<html><body><p>Hi All, <br><br> As of today, overall code coverage of '+orgName+' Org is Below </p><table border=1 cellpadding=10 style=border-collapse:collapse; > <tr> <th>SL No.</th> <th>Instance</th> <th>Code Coverage in %</th> </tr> <tr> <td align=center>1</td> <td align=center>'+orgName+'</td> <td align=center>'+ intTotalPercentageCovered +'</td> </tr></table> <p> Thanks & Best Regards,<br>HCL Managed Service Team </p></body></html>'; email1.setHtmlBody(messageBody); email1.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc, csvAttc1}); Messaging.SendEmailResult [] r1 = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email1}); //Below code addere to remover the test checker job List<CronTrigger> crt = [SELECT id FROM CronTrigger where CronJobDetail.Name like '%Test Run Job Checker%']; if(crt.size()>0){ for(CronTrigger cr:crt){ System.abortJob(cr.id); } } }
Thanks & Regards,
Javwad Azeem
please check for Test Class Generator App from APP Exchange.Please refer the below link.
- https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD
- Please follow below Link on Test class for @future HTTP callout method.
- https://salesforce.stackexchange.com/questions/25408/how-to-get-test-coverage-for-future-and-http-response-method-in-class
- https://salesforce.stackexchange.com/questions/65653/how-to-test-a-future-method-which-makes-callouts
I hope it will be helpful.Please mark it as best answer if the information is informative.
Best Regards
Rahul Kumar
Please check once below URL :
http://salesforceprasad.blogspot.sg/2017/05/test-class-for-xml-dom-parser-and.html
Thanks
Varaprasad