You need to sign in to do that
Don't have an account?
Indianajones
help me please with code coverage
i have apex class and test class. topic: https://developer.salesforce.com/forums/ForumsMain?id=9062I000000gGOwQAM
I need code coverage 75% , but i new in salesforce and dont understand how up code coverage. help me please.
(Opportunity.Invoice_Number__c - custom field auto number)
test
thanks
I need code coverage 75% , but i new in salesforce and dont understand how up code coverage. help me please.
(Opportunity.Invoice_Number__c - custom field auto number)
global with sharing class controllerTest { public Opportunity opportunity {get; set;} public List<OpportunityLineItem> oppLineItem {get; set;} public list<OpportunityContactRole> contact {get; set;} @AuraEnabled public static void savePDF(id recordId){ Opportunity opps = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId]; List<ContentVersion> contentVersionss = [SELECT ContentDocumentId FROM ContentVersion WHERE Title =: opps.Invoice_Number__c]; if(!contentVersionss.isEmpty()){ Id contentDocumentId = contentVersionss.get(0).ContentDocumentId; Opportunity opp = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId]; PageReference pdfPage = new PageReference('/apex/newPageVf'); pdfPage.getParameters().put('Id', recordId); Blob pdfBlob; if(Test.isRunningTest()) { pdfBlob = blob.valueOf('Unit.Test'); } else { pdfBlob = pdfPage.getContentAsPDF(); } String fileName = opp.Invoice_Number__c; List<ContentVersion> contentVersions = [SELECT ContentDocumentId FROM ContentVersion WHERE Title =: fileName]; ContentVersion cv = new ContentVersion(); cv.Title = fileName; cv.PathOnClient = fileName + '.pdf'; cv.VersionData = pdfBlob; cv.contentDocumentId = contentDocumentId; INSERT cv; } else { Opportunity opp = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId]; PageReference pdfPage = new PageReference('/apex/newPageVf'); pdfPage.getParameters().put('Id', recordId); Blob pdfBlob; if(Test.isRunningTest()) { pdfBlob = blob.valueOf('Unit.Test'); } else { pdfBlob = pdfPage.getContentAsPDF(); } // Blob pdfBlob = pdfPage.getContentAsPDF(); String fileName = opp.Invoice_Number__c; ContentVersion cv = new ContentVersion(); cv.Title = fileName; cv.PathOnClient = fileName + '.pdf'; cv.VersionData = pdfBlob; INSERT cv; Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id].ContentDocumentId; Id opportunId = [SELECT Id FROM Opportunity WHERE id =: recordId].Id; ContentDocumentLink cdl = new ContentDocumentLink(); cdl.ContentDocumentId = contentDocumentId; cdl.LinkedEntityId = opportunId; cdl.ShareType = 'V'; INSERT cdl; } } public controllerTest(ApexPages.StandardController stdController){ Id oppId = apexpages.currentpage().getparameters().get('id'); this.opportunity = [SELECT Id, Name, OwnerId, AccountId, Amount, Invoice_Number__c FROM Opportunity WHERE Id =: oppId]; this.oppLineItem = [SELECT OpportunityId, Name, Quantity, UnitPrice, TotalPrice FROM OpportunityLineItem WHERE OpportunityId =: oppId]; this.contact = [SELECT id, ContactId, Contact.Name, Contact.Phone, Contact.Email, IsPrimary, Contact.Account.Name FROM OpportunityContactRole WHERE OpportunityId =: oppId AND IsPrimary = true]; } }
test
@isTest public class controllerTestTest{ @isTest public Static Void UnitTest(){ Account Acc =new Account(); Acc.Name = 'Account test'; Insert Acc; Opportunity opp = new Opportunity(); opp.Name = 'test opp'; opp.Stagename = 'Closed Won'; opp.CloseDate = System.today(); opp.AccountId = Acc.Id; insert Opp; Opportunity opp1 = new Opportunity(); opp1.Name = 'test opp'; opp1.Stagename = 'Closed Won'; opp1.CloseDate = System.today(); opp1.AccountId = Acc.Id; insert opp1; ContentVersion contentVersionInsert = new ContentVersion( Title = 'Test', PathOnClient = 'Test.jpg', VersionData = Blob.valueOf('Test Content Data'), IsMajorVersion = true ); insert contentVersionInsert; ContentVersion contentVersionInsert1 = new ContentVersion( Title = 'sample1', PathOnClient = 'Test.jpg', VersionData = Blob.valueOf('Test Content Data'), IsMajorVersion = true ); insert contentVersionInsert1; ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersionInsert.Id LIMIT 1]; List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument]; System.assertEquals(documents.size(), 2); controllerTest.savePDF(opp.id); controllerTest.savePDF(opp1.id); } }
thanks
To get your coverage up I think you need 2 things. 1) one of your opportunities in your test class needs to have a invoice number that is the same as the title of one of your ContentVersions 2) you need to call the class constructor. To do this second part you need to change the assignment of oppId in public controllerTest(ApexPages.StandardController stdController) to be :-
(this is the correct way to get the id for a standard controller)
The test class can then be:-
There are plenty of other improvements which could be made to both classes but that will get your coverage to 95%.