Creating The Test Class For An Apex Extension.

When faced with the challenge of writing test cases for an extension, which can be a somewhat challenging task, this post serves as a helpful guide.

When there is a need to enhance the functionality of a Visualforce page, the go-to option is to utilize Apex Extensions.

Extensions prove to be exceptionally beneficial in expanding the capabilities of a Visualforce page.

If the business requirement is to retrieve a single record and display its fields, Standard Controllers are the solution.

Additionally, the standard controller provides access to 9 methods that can be used without any additional concerns.

However, what if there is a requirement to add a custom method on top of the existing functionality?

This is where extensions come into play.

The challenge arises when writing the test class for extensions, as extensions typically have a one-argument constructor.

Here’s the approach to writing the test class for extensions.

Account testAccount = new Account(Name='Test Company Name123');
insert testAccount;

ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
newAccountPlan testAccPlan = new newAccountPlan(sc);

// AccountPlan is the name of the Visualforce page
PageReference pageRef = Page.AccountWizardPage;
// We will be passing the Id in the URL so am gonna pass it here
pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
Test.setCurrentPage(pageRef);