function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
IndianajonesIndianajones 

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)
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);
        
    }
}
User-added image

thanks
Andee Weir 24Andee Weir 24
To see what your test class is touching & what it is not, run the test class in dev console & then switch to show the main apex class.  You will see a dropdown box towards the top left of the dev console which by default says 'code coverage:none'.  Change that to be your test class. Lines shown in red your test class is not running, lines in blue it is.

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 :-
Id oppId = stdController.getId();

(this is the correct way to get the id for a standard controller)

The test class can then be:-
@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.Invoice_Number__c = 'Test';
        opp.AccountId = Acc.Id;
        insert Opp;
        Opportunity opp1 = new Opportunity();
        opp1.Name = 'test opp';
        opp1.Invoice_Number__c = 'missing';
        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);
        
        PageReference pageRef = Page.controllerTestPage;
        Test.setCurrentPage(pageRef);        
        pageRef.getParameters().put('id', String.valueOf(opp.Id));
        ApexPages.StandardController sc = new ApexPages.StandardController(opp);
        controllerTest ct = new controllerTest(sc);
        
        System.assertEquals(opp.name, ct.opportunity.name);
        
    }
}

There are plenty of other improvements which could be made to both classes but that will get your coverage to 95%.
Andee Weir 24Andee Weir 24
Just spotted your comment that Invoice_Number__c is an autonumber field.  In which case after the insert of the opportunities you'll need to do a further SOQL to get the Invoice Numer for the opportunities & then use one of those values as the Title when creating one of the the ContentVersions