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
Jonathan Wolff 7Jonathan Wolff 7 

Test class for pdf creation Apex

Hello, could you help me with test class for my apex?
 
public class MonthlyReport4FDB{
 
@InvocableMethod(Label = 'MonthlyReport4FDB' description='')  
public static void MonthlyReport4FDB() {

     PageReference page = Page.MonthlyReport;
    
     Blob contentBlob = page.getContentAsPDF();
     
    ContentVersion cv = new ContentVersion();
    cv.VersionData = contentBlob;
    cv.Title =  System.today().year() + '_' + System.today().month() + '_FDB_Alle_Aktivitaeten';
    cv.PathOnClient =  System.today().year() + '_' + System.today().month() + '_FDB_Alle_Aktivitaeten.pdf';
    cv.Vertraulichkeitsstufe__c = 'Intern';
    cv.Dokumentenklasse__c ='Anderes Dokument (nicht aufbewahrungspflichtig)';
    insert cv;                
    cv = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id LIMIT 1];
        
    ContentDocumentLink cdl = new ContentDocumentLink();
    cdl.ContentDocumentId = cv.ContentDocumentId;
    cdl.ShareType = 'I';
    cdl.LinkedEntityId = '001b000000Zps12AAB';
    insert cdl;
    }
}

 
Govind TalariGovind Talari

Hi Jonathan,

You have to create the required data for the page MonthlyReport, and call the class MonthlyReport4FDB.MonthlyReport4FDB();

Here is an example :

  @isTest

public class testOpportunityController{
    public static testMethod void testOpp () {
        opportunityController oppC = new opportunityController ();
        pageReference pager = page.OpportunityPage; // PageReference page = Page.MonthlyReport;
        Test.setCurrentPage(pager);
        oppC.opp.Name  = 'abc';
        oppC.opp.Stagename = 'Prospecting';
        oppC.opp.Closedate = system.today();
        oppC.save();
       system.assert(oppC.opp.Id != null);

        apexPages.Currentpage().getParameters().put('Id',opp.id);
       
        MonthlyReport4FDB.MonthlyReport4FDB()

   }
}
Jonathan Wolff 7Jonathan Wolff 7
Hi Govin, I tried to build a test class but I'm stillat 0% with errors. Could you give me a full test class for this topic?
AbhinavAbhinav (Salesforce Developers) 
Check this:

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test 

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Thanks!