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
Ryan GreeneRyan Greene 

Apex Test for Invocable Method

Hello,
I am writing a test for an @InvocableMethod and for some reason I keep getting an error that says Invalid Type: SendPdf. Code below, any help is appreciated!!
Controller
public with sharing class EchoSign {
    private ApexPages.StandardController standardController;
    public EchoSign(ApexPages.StandardController standardController){
        this.standardController = standardController;}

@InvocableMethod
    public static void SendPdf(List<ID> conid){
        
    List<Contact> con = [SELECT Id, AccountId, Name FROM Contact WHERE Id =: conid LIMIT 1];  
        
        pageReference pdfPage = Page.ContactPDF;
        pdfPage.getParameters().put('Id',con[0].Id);
        blob pdfBody;
        pdfBody = pdfPage.getContentAsPDF();

//creation of Adobe Sign Agreement and Attachment here
}
}
Test
@isTest
public class Test_EchoSign2 {
    static testMethod void testechosign(){
    	Account ac = new Account(RecordTypeId = [SELECT Id, SobjectType, Name FROM Recordtype WHERE Name = 'Prospect/Client' AND SobjectType = 'Account' LIMIT 1].Id);
    	ac.Name = 'TestAcnt';
    	ac.Type = 'Prospect';
    	insert ac;
    
		Contact con = new Contact(RecordTypeId = [SELECT Id,SObjectType,Name FROM RecordType WHERE Name = 'Prospect/Client' AND SobjectType = 'Contact' LIMIT 1].Id);
		con.LastName = 'testLastName';
    	con.AccountId = ac.Id;
	    con.LeadSource = 'Unknown';
        insert con;
        
        Test.startTest();
        
        PageReference ContactPDF = Page.ContactPDF;
        ContactPDF.getParameters().put('Id',con.Id);
        Test.setCurrentPage(ContactPDF);
        
        SendPdf pdf = new SendPdf();
    }
}


 
Best Answer chosen by Ryan Greene
Tad Aalgaard 3Tad Aalgaard 3
It's method and not a class.  You cannot instancitate a method.

And, since it's a static method in a class you access it like so.

List<Contact> contacts = new List<Contact>();
contacts.add(con.Id);
EchoSign.SendPdf(contacts);

See here for more info
https://salesforce.stackexchange.com/questions/96131/how-to-write-a-test-class-for-an-invocablemethod

All Answers

Tad Aalgaard 3Tad Aalgaard 3
It's method and not a class.  You cannot instancitate a method.

And, since it's a static method in a class you access it like so.

List<Contact> contacts = new List<Contact>();
contacts.add(con.Id);
EchoSign.SendPdf(contacts);

See here for more info
https://salesforce.stackexchange.com/questions/96131/how-to-write-a-test-class-for-an-invocablemethod
This was selected as the best answer
Ryan GreeneRyan Greene
Thanks Tad
Your answer seems to have pushed me in the right direction. The test now errors when run, with the error Methods defined as TestMethod do not support getContent call. New Test looks like below; any thoughts on how to fix? I think the problem lies with the conid.add(con.id); line.
@isTest
public class Test_EchoSign2 {
    static testmethod void testechosign(){
    	Account ac = new Account(RecordTypeId = [SELECT Id, SobjectType, Name FROM Recordtype WHERE Name = 'Prospect/Client' AND SobjectType = 'Account' LIMIT 1].Id);
    	ac.Name = 'TestAcnt';
    	ac.Type = 'Prospect';
    	insert ac;
    
		Contact con = new Contact(RecordTypeId = [SELECT Id,SObjectType,Name FROM RecordType WHERE Name = 'Prospect/Client' AND SobjectType = 'Contact' LIMIT 1].Id);
		con.LastName = 'testLastName';
    	con.AccountId = ac.Id;
        con.Email = 'ryan.greene@outlook.com';
	    con.LeadSource = 'Unknown';
        insert con;
     
        List<Id> conid = new List<Id>();
        conid.add(con.Id);
		EchoSign.SendPdf(conid);
    }
}

 
Tad Aalgaard 3Tad Aalgaard 3
Are you sure that con is getting created? 

What do you see if you add the following after your insert con line?  Does it print out a Contact record?

System.debug('con : ' + con); 
Ryan GreeneRyan Greene
Finally figured it out!
It was creating a Contact, no issue there. The issue actually lied on the Class where the pdfBody = pdfPage.getContentAsPDF();

I had to substitue in:
if(Test.isRunningTest()){
            pdfBody = blob.valueOf('Unit.Test');
        }else{
            pdfBody = pdfPage.getContentAsPDF();}
That way it does not ".getContentAsPDF()" which is where the test was failing. Your original answer was the solution, just didn't account for the rewrite of the controller.
Thanks Tad!