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
Shawn Reichner 29Shawn Reichner 29 

Test Class Error - Please help

I have created a VF Page to replicate a form that we use, with an extension to when the command button is pressed on the VF page will attach the form to the record the button to pull up the VF page was caled from. 

I am now trying to test my test class and I am getting the following error message upon testing.  "Methods defined as TestMethod do not support getContent call"

Can anyone help re-write or give suggestions on how to get this test class to test out the controller extension? 

Appreciate any help!

Extension Code:
public class ArmorIncMDNAExtension {

    private final Contract__c contract;
    
    private ApexPages.StandardController standardController;
    
    public ArmorIncMDNAExtension(ApexPages.StandardController standardController) {
        this.Contract = (Contract__c)standardController.getRecord();
    }
    
    public pagereference GeneratePDF(){
pagereference Pg = Page.ArmorIncMDNA;
Blob pdf1 = pg.getcontentAsPdf();
Attachment d = new Attachment();
d.ParentId = contract.Id;
d.Body = Pdf1;
d.Name = contract.Account__r.Name + ' Armor Defense Inc. Mutual NDA.pdf';
d.ContentType = 'application/pdf';
insert d;
return null;
}
    
}

Test class Code:
@isTest
public class ArmorContractExtTest {

    
    
    static testMethod void tm1 () {

		Date myDate = Date.newInstance(2017, 8, 17);
        
        Contract__c c = new Contract__c();
        	c.Account__c = '001S000000rwY8H';
        c.Auto_Renew__c = 'No';
        c.Contract_Term_In_Months__c = 12;
        c.Effective_Date__c = myDate;
        c.Renewal_Period_Term_In_Months__c = 12;
        
        insert c;
         
        ArmorIncMDNAExtension ae = new ArmorIncMDNAExtension(new ApexPages.StandardController(c));
        ae.GeneratePDF();

    }
    
    
}

Best Answer chosen by Shawn Reichner 29
Amit Chaudhary 8Amit Chaudhary 8
Please change your code like below
public class ArmorIncMDNAExtension {

	private final Contract__c contract;
	private ApexPages.StandardController standardController;
	public ArmorIncMDNAExtension(ApexPages.StandardController standardController) {
		this.Contract = (Contract__c)standardController.getRecord();
	}

	public pagereference GeneratePDF()
	{
		pagereference Pg = Page.ArmorIncMDNA;
	
		Blob pdf1;
		if (Test.IsRunningTest()){
			pdf1=Blob.valueOf('UNIT.TEST');
		}
		else{
			pdf1 = pg.getcontentAsPdf();
		}
	
		Attachment d = new Attachment();
		d.ParentId = contract.Id;
		d.Body = Pdf1;
		d.Name = contract.Account__r.Name + ' Armor Defense Inc. Mutual NDA.pdf';
		d.ContentType = 'application/pdf';
		insert d;
	return null;
	}
    
}

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
getcontentAsPdf method is not supported in Test class so please update your code like below
public class ArmorIncMDNAExtension {

	private final Contract__c contract;
	private ApexPages.StandardController standardController;
	public ArmorIncMDNAExtension(ApexPages.StandardController standardController) {
		this.Contract = (Contract__c)standardController.getRecord();
	}

	public pagereference GeneratePDF()
	{
	
		Blob pdf1;
		if (Test.IsRunningTest()){
			pdf1=Blob.valueOf('UNIT.TEST');
		}
		else{
			pdf1 = pg.getcontentAsPdf();
		}
	
		pagereference Pg = Page.ArmorIncMDNA;
		Attachment d = new Attachment();
		d.ParentId = contract.Id;
		d.Body = Pdf1;
		d.Name = contract.Account__r.Name + ' Armor Defense Inc. Mutual NDA.pdf';
		d.ContentType = 'application/pdf';
		insert d;
	return null;
	}
    
}

Try to create all test data in your test class like below
@isTest
public class ArmorContractExtTest 
{
    static testMethod void tm1 () {

		Account acc = new Account();
			acc.Name ='Test';
		insert acc;

		Date myDate = Date.newInstance(2017, 8, 17);

        Contract__c c = new Contract__c();
			c.Account__c = acc.id ;
			c.Auto_Renew__c = 'No';
			c.Contract_Term_In_Months__c = 12;
			c.Effective_Date__c = myDate;
			c.Renewal_Period_Term_In_Months__c = 12;
        insert c;
         
        ArmorIncMDNAExtension ae = new ArmorIncMDNAExtension(new ApexPages.StandardController(c) );
        ae.GeneratePDF();

    }
    
    
}

Let us know if this will help you
 
Shawn Reichner 29Shawn Reichner 29
Amit, thank you for your insight and help sir.  This did not allow for successful testing either.  I applied your changes and now I am getting a different error, which is Variable does not exist: pg for line 17 in the ArmorIncMDNAExtension class.   Can you look this over and see if you can help further? 

Thank you again for your attempt to assist I really appreciate it!

Shawn
Amit Chaudhary 8Amit Chaudhary 8
Please change your code like below
public class ArmorIncMDNAExtension {

	private final Contract__c contract;
	private ApexPages.StandardController standardController;
	public ArmorIncMDNAExtension(ApexPages.StandardController standardController) {
		this.Contract = (Contract__c)standardController.getRecord();
	}

	public pagereference GeneratePDF()
	{
		pagereference Pg = Page.ArmorIncMDNA;
	
		Blob pdf1;
		if (Test.IsRunningTest()){
			pdf1=Blob.valueOf('UNIT.TEST');
		}
		else{
			pdf1 = pg.getcontentAsPdf();
		}
	
		Attachment d = new Attachment();
		d.ParentId = contract.Id;
		d.Body = Pdf1;
		d.Name = contract.Account__r.Name + ' Armor Defense Inc. Mutual NDA.pdf';
		d.ContentType = 'application/pdf';
		insert d;
	return null;
	}
    
}

Let us know if this will help you
 
This was selected as the best answer
Shawn Reichner 29Shawn Reichner 29
Amit,

Thank you sir!  That did the trick and now I have 93 % code coverage.   Thank you again for your awesome help!

Shawn