You need to sign in to do that
Don't have an account?
Unit Tests for Apex REST Services
I have a REST service but i am not able to cover this class.
Below is my code, need help ASAP.
Thanks You in advance.
Test class:
Below is my code, need help ASAP.
Thanks You in advance.
@RestResource(urlMapping='/AttachPDF/*') global class ServiceInvoiceRedirectController { @HttpGet global static void AttachPDFtoRecordREST() { List<Id> accId = new List<Id>(); List<String> emailList = new List<String>(); RestRequest req = RestContext.request; id recordId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); PageReference pdfPage = new PageReference('/apex/ServiceInvoicePDF?Id='+recordId); pdfPage.getParameters().put('id',recordId); Blob pdf = pdfPage.getContentAsPdf(); //!Test.isRunningTest() ? pdfPage.getContentAsPdf() : Blob.ValueOf('dummy text'); //List of accoutn Id's of related project for(Account_Project_Junction__c accPrjJunRec: [SELECT Id,Project__c,Account__c From Account_Project_Junction__c WHERE Project__c =: recordId]){ accId.add(accPrjJunRec.Account__c); } //List of email id's of the selected account's for(Contact conRec: [SELECT Id,AccountID,Email,Is_Primary_Contact__c From Contact where AccountID IN: accId AND Is_Primary_Contact__c = true]){ if(conRec.Email != null){ emailList.add(conRec.Email); } } Project__c prjRec = [Select Id,Name from Project__c where Id =: recordId]; //Insert Invoice record for related project Invoice__c invoRec = new Invoice__c(); invoRec.RecordTypeId = Constants_PicklistVariables.serInvoice; invoRec.Project__c = recordId; try{ insert invoRec; }catch(Exception e){ System.debug('Invoice=Exception-->'+e); } System.debug('Invoice=invoRec.id-->'+invoRec); //Insert Attacment to the invoice Attachment a = New Attachment(); a.body = pdf; a.parentID = invoRec.id; a.ContentType = 'application/pdf'; a.Name = 'SER-INV-'+invoRec.id+'.pdf'; try{ insert a; }catch(Exception e){ System.debug('Attachment=Exception-->'+e); } //Create the email attachment Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName('SER-INV-'+invoRec.id+'.pdf');//Pleace ad pdf name from contact name project and oppty concatinate efa.setContentType('application/pdf'); efa.setBody(pdf); // Create the Singal Email Message Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); email.setSubject('Service Invoice Notification'); String[] CCEmails = emailList; email.setToAddresses(CCEmails); //email.setToAddresses(new String[] { 'developer@extentor.com' });//add Contact email id email.setPlainTextBody( 'Dear:Sir/Madam <br/> Please Find Attachment your financial report Attached with this email.'); email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); // Sends the email Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); } // call this method from your Batch Apex global static void attachPdfToRecord( Id recordId, String sessionId ) { System.debug('recordId==>'+recordId); System.debug('sessionId==>'+sessionId); System.debug('Iam in REST API Call method'); HttpRequest req = new HttpRequest(); req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/apexrest/AttachPDF/'+recordId); req.setMethod('GET'); req.setHeader('Authorization', 'OAuth ' + sessionId); Http http = new Http(); HttpResponse response = http.send(req); } }
Test class:
@isTest(seeAllData=false) global class Test_ServiceInvoiceRedirectController{ static testMethod void testDoGet() { Account acc= InitialTestData.createAccount('Raj'); insert acc; Contact contact1=InitialTestData.createContact(acc.id,'Sen','raj@gmail.com'); insert contact1; Opportunity_Deal__c oppdeal=InitialTestData.createOpportunityDeal('Opp1',acc.id,contact1.id,false,'Quote'); insert oppdeal; Project__c project1=InitialTestData.createPlotProject('Pro',acc.id,'Submitted',oppdeal.id); insert project1; RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); // pass the req and resp objects to the method req.requestURI = URL.getSalesforceBaseUrl().toExternalForm()+'/services/apexrest/AttachPDF/'+project1.Id; req.httpMethod = 'GET'; ServiceInvoiceRedirectController.attachPdfToRecord results = ServiceInvoiceRedirectController.AttachPDFtoRecordREST(req,res); System.assertEquals('true', results.success); System.assertEquals(10, results.records.size()); System.assertEquals('Query executed successfully.', results.message); } }
the importent was the "req" url gives the record id to get that id was importent*
Mock Test Class:(The same code)
Test Class
All Answers
the importent was the "req" url gives the record id to get that id was importent*
Mock Test Class:(The same code)
Test Class