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
AlisonRAlisonR 

Test Class for Standard Controller Extension

I'm new to VF and created my first VF extension:

 

public class LeadSalesActH {
    
    private final Lead ld;
    
    public LeadSalesActH(ApexPages.StandardController stdController) {
        this.ld = (Lead)stdController.getRecord();
    }

    public String getName() {
        return 'LeadSalesActH';
    }
        
    public Lead getLead() {
        return [select id, name, ownerid,
                    (select subject, whatid, whoid, activitydate, LVM__c, ownerid, lastmodifieddate from ActivityHistories 
                     where ownerid not in ('00530000000vpI1') order by activitydate DESC ) //filter out marketing actvities 
                from Lead
                where id = :ApexPages.currentPage().getParameters().get('id')];
    }

}

 I started on my test class but I'm getting stuck on the getLead() tests. Can someone put me in the right direction?

 

Test Class:

 

@isTest
private class ExtendObjectTest {

    static testmethod void constructorTest() {
        // set up some test data to work with
        Lead ld = new Lead(LastName='Test', Company='Test1', Status='New');
        insert ld;

        // start the test execution context
        Test.startTest();

        // set the test's page to your VF page (or pass in a PageReference)
        Test.setCurrentPage(Page.leadsales);

        // call the constructor
        LeadSalesActH controller = new LeadSalesActH(new ApexPages.StandardController(ld));

        // stop the test
        Test.stopTest();
    }

}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
VennilaVennila

Hi,

 

you need to call those functions from your test class. Try this now.

 

 

@isTest
private class ExtendObjectTest {

static testmethod void constructorTest() {
// set up some test data to work with
Lead ld = new Lead(LastName='Test', Company='Test1', Status='New');
insert ld;

// start the test execution context
Test.startTest();

// set the test's page to your VF page (or pass in a PageReference)
Test.setCurrentPage(Page.leadsales);

// call the constructor
LeadSalesActH controller = new LeadSalesActH(new ApexPages.StandardController(ld));
controller.getName();
controller.getlead();

// stop the test
Test.stopTest();
}

}

All Answers

VennilaVennila

Hi,

 

you need to call those functions from your test class. Try this now.

 

 

@isTest
private class ExtendObjectTest {

static testmethod void constructorTest() {
// set up some test data to work with
Lead ld = new Lead(LastName='Test', Company='Test1', Status='New');
insert ld;

// start the test execution context
Test.startTest();

// set the test's page to your VF page (or pass in a PageReference)
Test.setCurrentPage(Page.leadsales);

// call the constructor
LeadSalesActH controller = new LeadSalesActH(new ApexPages.StandardController(ld));
controller.getName();
controller.getlead();

// stop the test
Test.stopTest();
}

}

This was selected as the best answer
AlisonRAlisonR

Awesome, that worked perfectly.

 

Follow up question:

 

I'm now getting an error when I run the test "System.QueryException: List has no rows for assignment to SObject" is there something I need to add to my controller extension to stop this from failing?

AlisonRAlisonR

Nevermind, I figured it out. Here's my modified Controller Extension:

 

public class LeadSalesActH {
    
    private final Lead ld;
    
    public LeadSalesActH(ApexPages.StandardController stdController) {
        this.ld = (Lead)stdController.getRecord();
    }

    public String getName() {
        return 'LeadSalesActH';
    }
        
    public Lead getLead() {
        Lead[] ld = [select id, name, ownerid,
                    (select subject, whatid, whoid, activitydate, LVM__c, ownerid, lastmodifieddate from ActivityHistories 
                     where ownerid not in ('00530000000vpI1') order by activitydate DESC) //filter out marketing actvities 
                from Lead
                where id = :ApexPages.currentPage().getParameters().get('id')]; if (ld.size() > 0) {return ld[0];} else {return null;
    }

}}