You need to sign in to do that
Don't have an account?
Andreas 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
All Answers
Get this error on line 41:
Which is the following line:
Thanks for the help Raj!