You need to sign in to do that
Don't have an account?
Joseph Barone
Custom Controller Test Class Successful, but 0 code coverage
Hello,
I've been beating my head against a brink wall for a few days now. I've gotten the test class for my custom controller to run successfully, but get zero code coverage. I've read so many different website and forums that I do not know what is correct. I've pasted both my Controller and Test Class below.
Thank you!
I've been beating my head against a brink wall for a few days now. I've gotten the test class for my custom controller to run successfully, but get zero code coverage. I've read so many different website and forums that I do not know what is correct. I've pasted both my Controller and Test Class below.
Thank you!
public class Tier1Report { public string idCurrentUser {get;set;} Public List <Contact> filteredContact; Public List <CM_Relationship__c> filteredCMrelationshipcontact; Public List <Contact> cmContacts{get;set;} Public List <Contact> selected {get;set;} public Tier1Report () { idCurrentUser = UserInfo.getUserId(); system.debug(idCurrentUser); selected=new list<Contact>(); filteredContact = [Select ID, name,accountID,Weeks_Last_Contacted__c FROM Contact Where ownerid = :idCurrentuser AND Referral_Tier__c='1 - Direct']; filteredCMrelationshipcontact = ([Select ID, Contact__c From CM_Relationship__c where Owner__c = :idCurrentuser]); Set<String> CMstringID = new Set<String>(); for(integer i=0;i<filteredCMrelationshipcontact.size();i++) { CMStringID.add(filteredCMrelationshipcontact.get(i).Contact__c); } Map<Id,Contact> CMcontact = new Map<Id,Contact>([Select ID, name,accountID,Weeks_Last_Contacted__c FROM Contact Where Id IN :CMStringID AND Referral_Tier__c='1 - Direct']); system.debug(CMcontact); for (Contact c:filteredContact) { if(c.weeks_last_contacted__c >= 2) { if(CMcontact.containsKey(c.id)==False) { selected.add(c); } } } for(Contact cmap: CMcontact.values()) { if(cmap.Weeks_Last_Contacted__c >= 2) { selected.add(cmap); } } } }
@isTest private class Tier1Test { static user testuser; static testMethod void validateTier1Report(){ profile p =[select id from profile where name='All but IT']; testUser = new User(alias = 'u1', email='u1@testorg.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, country='United States', timezonesidkey='America/Los_Angeles', username='u1@testorg.com'); insert testUser; string uid = testuser.Id; Tier1Report controller = new Tier1Report(); controller.idcurrentuser = uid; Account ac = new Account(); ac.name = 'Testing class'; ac.phone = '5555555555'; ac.industry = 'ASP'; ac.website = 'http://www.testing.com'; ac.ownerid = uid; insert ac; system.assertEquals('Testing class',ac.name); string Accountid = ac.id; Contact nc = new Contact(); nc.lastname = 'Testing Name'; nc.accountid = Accountid; nc.referral_tier__c = '1 - Direct'; nc.ownerid = uid; insert nc; system.assertEquals('1 - Direct',nc.referral_tier__c); string Contactid = nc.id; string userid2 = nc.ownerid; Event ne = new Event (); ne.subject = 'Testing Class works'; ne.whoid = contactid; ne.startdatetime = datetime.newinstance(2015,2,1,8,5,0); ne.enddatetime = datetime.newinstance(2015,2,1,9,5,0); insert ne; system.assertEquals('Testing Class works',ne.subject); CM_Relationship__c ncm = new CM_Relationship__c (); ncm.contact__c = contactid; ncm.owner__c = uid; insert ncm; test.starttest(); List <Contact> ctest = [Select ID, name FROM Contact Where Referral_Tier__c='1 - Direct']; List <CM_Relationship__c> cmtest = [Select id, owner__c, contact__c From CM_Relationship__c Limit 1]; test.stoptest(); system.AssertEquals(1,ctest.size()); system.assertEquals(1,cmtest.size()); } }
All Answers
Move your controller invocation (line 19) down below line 59, (after you stage your data) see if that helps you out at all.
Thank you for the suggestion. Unfortunately, it did not work. It still was successful, but has 0 code coverage.
Based off your initial sentence are you suggesting that I should re-write my Controller in a different way, which in turn might help with the testclass issue?
Thank you
Normally I hate having code in the constructors - as in good OO practice, other things could be calling your class and you don't want to lock yourself in anywhere. =)