Test Coverage For PDF Attachment

Over the years, I’ve received several emails specifically inquiring about test coverage for the PDF logic demo. Today, I finally have some time to document it.

/*
	Created by: Greg Hacic
	Last Update: 14 June 2018 by Greg Hacic
	Questions?: greg@interactiveties.com
	
	Notes:
		- test for attachPDFToAccount.cls (92.31% coverage)
*/
@isTest
private class attachPDFToAccountTest {
	
	//tests attachPDFToAccount
	@isTest //defines method for use during testing only
	static void attachPDFLogic() {
		//BEGIN: Some Setup Items...
		List<Account> accounts = new List<Account>();
		accounts.add(new Account(Name = 'Tess Trucking Co.'));
		insert accounts;
		//END: Some Setup Items...
		
		Test.startTest(); //denote testing context
		
		PageReference pageRef = Page.attachPDFToAccount; //create a page reference to attachPDFToAccount.page
		Test.setCurrentPage(pageRef); //set page context
		ApexPages.StandardController standardController = new ApexPages.standardController(accounts[0]); //instantiate the standard Account object controller
		attachPDFToAccount ext = new attachPDFToAccount(standardController); //instantiate the extension
		
		String validationURLString = ext.attachPDF().getURL(); //get the URL that is returned after the attachPDF() method is invoked
		String accountIdAsString = Id.valueOf(accounts[0].Id); //variable represtenting the Account record Id as a String
		System.assertEquals(true, validationURLString.contains(accountIdAsString.left(15))); //validate that the URL contains the Id of the Account record
		
		Test.stopTest(); //revert from testing context
	}

}

There isn’t much to emphasize here.

However, I do value it when readers pose questions. Feel free to continue sending them my way.