+ Start a Discussion
Joseph BaroneJoseph 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!
 
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());
        
   
    }
}

 
Best Answer chosen by Joseph Barone
Andy BoettcherAndy Boettcher
Where are you testing/viewing code coverage?  There IS a "bug" right now if you're doing it via developer console where you need to uncheck the "Test/Always Run Asynchronously" checkbox.

All Answers

Andy BoettcherAndy Boettcher
Half of the problem is that all of your code is in the class constructor, so when you invoke your controller on line 19 of the test class BEFORE you set up any of your data - it has no data to go against.

Move your controller invocation (line 19) down below line 59, (after you stage your data) see if that helps you out at all.
Joseph BaroneJoseph Barone
Andy,

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?
Andy BoettcherAndy Boettcher
Where are you testing/viewing code coverage?  There IS a "bug" right now if you're doing it via developer console where you need to uncheck the "Test/Always Run Asynchronously" checkbox.
This was selected as the best answer
Joseph BaroneJoseph Barone
I had to adjust the test setting and that started to return results.  I now just have to adjust everything to get better code coverage than 54%, it's a start.  

Thank you
Andy BoettcherAndy Boettcher
As a "best practice" note - I would move that logic out of the controller and instead use the get/set automatics to hold that information.  Read up on that here:  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_properties.htm

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.  =)
Niko SmrekarNiko Smrekar
Thank you, Andy for the asynchronous hint. I have been hitting my head against this wall for a week and finally got complete coverage.