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
BaguiarBaguiar 

70% test coverage on controller....

Hello there,

 

I'm getting 70% coverage with the test below, to a class working as a controller to a VF page. Dunno what am I missing and the allways good help is appreciated! :)

 

Class:

public class KeyLeadership
{

 
  private List<Leadership_position_code__c> Leaders;
  public List<Leadership_position_code__c> getLeaders()
    
    

    {
      Leaders = [ Select l.Contact__c, l.Contact__r.AccountId,  l.Contact__r.Email, l.Contact__r.Name, l.Position__c, l.Start_Date__c, l.Thru_Date__c from Leadership_Position_Code__c l WHERE (Contact__r.AccountId = :ApexPages.currentPage().getParameters().get('id') ) and 

    (l.Thru_Date__c = null OR l.Thru_Date__c > :system.today()) and ( l.Position__c = 'President' OR l.Position__c = 'Education Director' OR l.Position__c = 

'Co-President' OR l.Position__c = 'Senior (or only) Rabbi' OR l.Position__c = 'Administrator/Exec director' )
            ];
            
            system.debug('%%%%%%%%%55555'  + leaders.size());
     return Leaders;
    }
            
}

 

TEst Class:

@isTest
private  class TestKeyLeadership
{
   static testMethod void testGetLeaders()
   {
      // insert a test account
      Account acc=new Account(Name='Test123');
      Database.saveresult sr = database.insert(acc);
     

      Contact con=new Contact(FirstName='Lester', Lastname='DbigTester', accountID=acc.id, email='123123123@888555.com');
      insert con;

      List<leadership_position_code__c> LDS=new List<leadership_position_code__c>{new leadership_position_code__c(Contact__C=con.id, position_type__C='Clergy', Position__C='Cantor', Start_Date__C= system.Today()-2, Thru_date__C= system.Today()-1 ),
       new leadership_position_code__c(Contact__C=con.id, position_type__C='Officer', Position__C='President', Start_Date__C= system.Today()-1 ), 
       new leadership_position_code__c(Contact__C=Con.id, position_type__C='Clergy', Position__C='Senior (or only) Rabbi', Start_Date__C=system.Today()-1 )};
      insert LDS;
     
      ApexPages.currentPage().getParameters().put('id', acc.id);


      // instantiate the class under test
      KeyLeadership kl=new KeyLeadership();
     
      List<Leadership_Position_code__c> leaders=kl.getLeaders();

      // change the number below to the number of leadership_position_code__c objects created above
      System.assertEquals(2, leaders.size());

      // should also assert that the inserted records above match those retrieved.
      Leadership_position_code__C[] LDSok = [select id,position__C from Leadership_position_code__C where id in :leaders];
    System.assertEquals('President',LDSok[0].Position__c);
    System.assertEquals('Senior (or only) Rabbi',LDSok[1].Position__c);
      
      
   }
}

 

Thanks in advance!

B

 

Best Answer chosen by Admin (Salesforce Developers) 
MJ09MJ09

Are you sure the 70% coverage you're getting is just on the controller, or is 70% coverage of all Apex code in your org? If your Leadership_Position_Code__c object has a trigger, the 70% number might include coverage of the trigger as well as the controller.

 

After you run the unit tests from the browser, scroll down to the section titled "Code Coverage." You should see the percent of code coverage for each Apex class and trigger. If you click on the percentage number, a pop-up window will appear to show you exactly which lines of code did and didn't get covered.

All Answers

MJ09MJ09

Are you sure the 70% coverage you're getting is just on the controller, or is 70% coverage of all Apex code in your org? If your Leadership_Position_Code__c object has a trigger, the 70% number might include coverage of the trigger as well as the controller.

 

After you run the unit tests from the browser, scroll down to the section titled "Code Coverage." You should see the percent of code coverage for each Apex class and trigger. If you click on the percentage number, a pop-up window will appear to show you exactly which lines of code did and didn't get covered.

This was selected as the best answer
Jon Mountjoy_Jon Mountjoy_

In the UI, when you run the tests, and it says "70%", you can actually click on it to see what you are missing in terms of test coverage.  

 

See my blog post for an example.

Jon Mountjoy_Jon Mountjoy_

In the UI, when you run the tests, and it says "70%", you can actually click on it to see what you are missing in terms of test coverage.  

 

See my blog post for an example.

BaguiarBaguiar

It worked! Thats what it was . There was a class in my prod environment that had 0 coverage and I have no Idea how did it make to prod. I know it was a package from Appexchange (lastviewed) but the coverage was showing me 0%. I removed that and now my code on deplyments is 100%.

 

Thanks for the tip !