+ Start a Discussion
Andreas YannelosAndreas Yannelos 

Can't get test class to get code coverage on custom controller for visualforce page

Hi!

Have tried to get this to work for many hours now, but can't find a solution. I have written a cusom controller for a visualforce page that looks like the one below. And get 0% code coverage on the Apex Class.

Any idea what's wrong?

Controller:

public class outboundLeadStart {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Id, Name, Company, Country, CreatedDate, Status FROM Lead WHERE Status='New' AND OwnerId= :UserInfo.getUserID() ORDER BY CreatedDate DESC LIMIT 100]));
            }
            setCon.setPageSize(setCon.getResultSize());
            return setCon;
        }
        set;
    }
    
    public integer getCountLeads() {
            String statusStage = 'New';
            String ownerId = UserInfo.getUserID();
            return(Database.countQuery('SELECT count() FROM Lead WHERE Status= :statusStage AND OwnerId= :ownerId')); // Returns Integer        
    }
    
    // Initialize setCon and return a list of records
    public List<Lead> getLeads() {
        return (List<Lead>) setCon.getRecords();
    }
    
    public PageReference save() {
        
        try {
            update setCon.getRecords();
        } catch(DMLException e) {
            Apexpages.addmessages(e);
        }
        
        PageReference pageRef = new PageReference('/lightning/n/Outbound_Planning');
        pageRef.setRedirect(true);
        return pageRef;        
    }    
}

And the test class looks like this:
@isTest
private class testoutboundLeadStartTest {
    static testMethod void testoutboundLeadStartTest() 
    {
        User u = new User(
            ProfileId = [SELECT Id FROM Profile WHERE Name = 'Sales Representative'].Id,
            LastName = 'last',
            Email = 'last@example.com',
            Username = 'last@example.com' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US'
        );
        
        insert u;
        
        Id theOwnerId = UserInfo.getUserID();
        
        ApexPages.StandardSetController setCon;
        PageReference pageRef = Page.outboundLeadsStart;
        
        List <Lead> addedLeads = new List<Lead>();
        
        Lead testLead = new Lead();
        testLead.LastName='Test Lead';
        testLead.Company='Test Company1';
        testLead.Status='New';
        testLead.OwnerId=theOwnerId;
        insert testLead;
        
        List<Lead> cus = new List<Lead>{};
            
        Test.startTest();
        System.assertEquals('Test Lead', testLead.LastName);
        Test.setCurrentPage(pageRef);
        outboundLeadStart controller = new outboundLeadStart();
        Test.stopTest();
        
    }        
}
 

Can't understand why this get 0% code coverage on the Apex Class.

BR,
Andreas


 
Best Answer chosen by Andreas Yannelos
Raj VakatiRaj Vakati
Use this
 
@isTest
private class testoutboundLeadStartTest {
    static testMethod void testoutboundLeadStartTest() 
    {
        User u = new User(
            ProfileId = [SELECT Id FROM Profile WHERE Name = 'Sales Representative'].Id,
            LastName = 'last',
            Email = 'last@example.com',
            Username = 'last@example.com' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US'
        );
        
        insert u;
        
        Id theOwnerId = UserInfo.getUserID();
        
        ApexPages.StandardSetController setCon;
        PageReference pageRef = Page.outboundLeadsStart;
        
        List <Lead> addedLeads = new List<Lead>();
        for(Integer i = 0 ; i<10 ; i++){
        Lead testLead = new Lead();
        testLead.LastName='Test Lead';
        testLead.Company='Test Company1';
        testLead.Status='New';
        testLead.OwnerId=theOwnerId;
		addedLeads.add(testLead);
		}
        insert addedLeads;
        
 Test.startTest();
  Test.setCurrentPage(Page.YOUR_PAGE);
  ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(addedLeads);
  stdSetController.setSelected(addedLeads);
  outboundLeadStart ext = new outboundLeadStart(stdSetController);
  ext.getCountLeads();
  ext.getLeads();
  ext.save();
  
 Test.stopTest();
 
      
      
        
    }        
}

 

All Answers

Raj VakatiRaj Vakati
Use this
 
@isTest
private class testoutboundLeadStartTest {
    static testMethod void testoutboundLeadStartTest() 
    {
        User u = new User(
            ProfileId = [SELECT Id FROM Profile WHERE Name = 'Sales Representative'].Id,
            LastName = 'last',
            Email = 'last@example.com',
            Username = 'last@example.com' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US'
        );
        
        insert u;
        
        Id theOwnerId = UserInfo.getUserID();
        
        ApexPages.StandardSetController setCon;
        PageReference pageRef = Page.outboundLeadsStart;
        
        List <Lead> addedLeads = new List<Lead>();
        for(Integer i = 0 ; i<10 ; i++){
        Lead testLead = new Lead();
        testLead.LastName='Test Lead';
        testLead.Company='Test Company1';
        testLead.Status='New';
        testLead.OwnerId=theOwnerId;
		addedLeads.add(testLead);
		}
        insert addedLeads;
        
 Test.startTest();
  Test.setCurrentPage(Page.YOUR_PAGE);
  ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(addedLeads);
  stdSetController.setSelected(addedLeads);
  outboundLeadStart ext = new outboundLeadStart(stdSetController);
  ext.getCountLeads();
  ext.getLeads();
  ext.save();
  
 Test.stopTest();
 
      
      
        
    }        
}

 
This was selected as the best answer
Andreas YannelosAndreas Yannelos
Hi!

Get this error on line 41: 
 
Constructor not defined: [outboundLeadStart].<Constructor>(ApexPages.StandardSetController)

Which is the following line:
outboundLeadStart ext = new outboundLeadStart(stdSetController);

 
Andreas YannelosAndreas Yannelos
Found the problem; changed it to: 
outboundLeadStart ext = new outboundLeadStart();

Thanks for the help Raj!