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
ThathulaThathula 

How to run a test class for series of controller classes

Hi All,

I've senario like this. 
Have some series of winzard type service classes 
Once page one submitted it goes to a page two, page two submitted then it goes to page 3. like wise it goes until wizard ends. 
each page has submitNext (under that save())  and submitBack (Under that SaveBack()) to go to the Next page and Back page. 

each save and saveback functions call a common class method to access database and consume 5-6 queries. 

Is it possible me write a one test class and cover the test coverage of all the controllers, without exceeding SQL 101 limitation. 


Please help me 
Best regards. 


 
pconpcon
What I would do is to have a test method for each step.  Outside the Test.startTest/stopTest i would do any setup needed and if possible, fake the previous steps.  If not, then just run the step outside the startTest.  Once you hit Test.startTest it will reset all of your governor limits and you'll be in the clear.
ThathulaThathula
Thanks a lot for kind reply. May i know how do you mean by faking previous steps?  
pconpcon
So given the class below this is how I would fake the data
 
public class MyController {
    public Account acct { get; set; }
    public Contact con { get; set; }
    public Case kase { get; set; }
    public Entitlement ent { get; set; }

    public PageReference stepOne() {
        insert this.acct;

        this.ent = new Entitlement(
            Name = 'Trial entitlement',
            AccountId = this.acct.Id
        );
        return Page.WizardStepTwo;
    }

    public PageReference stepTwo() {
        this.acct = [
            select Custom_FormulaField__c
            from Account
            where Id = :this.acct.Id
        ];

        this.con.AccountId = this.acct.Id;
        this.con.CustomField__c = this.acct.Custom_FormulaField__c;
        insert this.con;

        return Page.WizardStepThree;
    }

    public PageReference stepThree() {
        this.kase.EntitlementId = this.ent.Id;
        this.kase.ContactId = this.con.Id;
        this.kase.AccountId = this.acct.Id;
        insert this.kase;

        return Page.WizardComplete;
    }
}

This class is a little silly but each step builds on the previous step.  So testing step one of the controller is pretty easy because it's just populating the this.acct variable and then making sure it gets inserted correctly.  But we don't want to have to run through step 1 and 2 to test step three.  So we would fake it like in the test method below
 
static testMethod void stepThreeTest() {
    Account testAccount = new Account(
        Name = 'Test Account'
    );
    insert testAccount;

    Contact testContact = new Contact(
        AccountId = testAccount.Id,
        FirstName = 'Bob',
        LastName = 'Dole',
        CustomField__c = 'Something'
    );
    insert testContact;

    Entitlement testEntitlement = new Entitlement(
        AccountId = testAccount.Id,
        Name = 'Trial entitlement'
    );
    insert testEntitlement;

    Case testCase = new Case(
        Subject = 'Test subject',
        Description = 'Test description'
    );

    MyController controller = new MyController();
    controller.acct = testAccount;
    controller.con = testContact;
    controller.ent = testEntitlement;

    Test.startTest();

    PageReference result = controller.stepThree();

    Test.stopTest();

    System.assertNotEquals(
        null,
        controller.kase.Id,
        'The case was not inserted'
    );

    System.assertEquals(
        testAccount.Id,
        controller.kase.AccountId,
        'The account was not set'
    );

    System.assertEquals(
        testContact.Id,
        controller.kase.ContactId,
        'The contact was not set'
    );

    System.assertEquals(
        testEntitlement.Id,
        controller.kase.EntitlementId,
        'The entitlement was not set'
    );
}

In the test above our governor limits will read as 0 for SOQL and 1 for DML for the code ran between the start/stopTest and will be 0 for SOQL and 3 for DML outside the start/stopTest.  If we did not fake the data (like the CustomField__c on the Contact) we would have a SOQL query.  This example doesn't really show how you could save a bunch of SOQL/DML but it does scale.