+ Start a Discussion
Chiho SullivanChiho Sullivan 

why isnt apex test covering controller code?

Im a newbie...what i need to do seems simple enough but i can't seem to get it to work.  hoping someone can help me correct my errors.  i have the following controller and test class to insert a new record into a custom object.  when i run tests and review my controller their is no blue or red marking showing me what is covered... also, code coverage percentage shows zero so im pretty sure the test is not happening.  HELP!

CONTROLLER:
public class KFGCreateTrainingReferralController {

    public Referrals__c referrals {get; set;}
   
   // blank constructor
    public KFGCreateTrainingReferralController() {
  referrals = new Referrals__c();
    }

  // save button is clicked
    public PageReference save() {
        try {
            upsert(referrals); // inserts the new record into the database
        } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new referral.'));
      return null;
        }
       
    // if successfully inserted new application, then displays the thank you page.
    return Page.TrainingReferralThankYou;
    }
}

TEST:

@isTest
public with sharing class KFGCreateTrainingReferralControllerTest {
    //==================== TEST METHOD(s) ======================================
    static testMethod void KFGCreateTrainingReferralControllerTest() {
 Referrals__c newReferrals = new Referrals__c();
 // Test insert Referrals__c
        newReferrals.Referral_Date__c = Date.Today();
        newReferrals.Referrer_First_Name__c = 'TestFirstName';
        newReferrals.Referrer_Last_Name__c = 'TestLastName';
        newReferrals.Email__c = 'testreferrer@test.com';
        newReferrals.State__c = 'ca';
  insert newReferrals;
    }
}
Best Answer chosen by Chiho Sullivan
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
 
@isTest
public with sharing class KFGCreateTrainingReferralControllerTest {
    //==================== TEST METHOD(s) ======================================
    static testMethod void KFGCreateTrainingReferralControllerTest() {
 Referrals__c newReferrals = new Referrals__c();
 // Test insert Referrals__c
        newReferrals.Referral_Date__c = Date.Today();
        newReferrals.Referrer_First_Name__c = 'TestFirstName';
        newReferrals.Referrer_Last_Name__c = 'TestLastName';
        newReferrals.Email__c = 'testreferrer@test.com';
        newReferrals.State__c = 'ca';
        insert newReferrals;
        KFGCreateTrainingReferralController controller=new KFGCreateTrainingReferralController();
        controller.save();
    }
}

Let us know if it helps.

All Answers

Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
 
@isTest
public with sharing class KFGCreateTrainingReferralControllerTest {
    //==================== TEST METHOD(s) ======================================
    static testMethod void KFGCreateTrainingReferralControllerTest() {
 Referrals__c newReferrals = new Referrals__c();
 // Test insert Referrals__c
        newReferrals.Referral_Date__c = Date.Today();
        newReferrals.Referrer_First_Name__c = 'TestFirstName';
        newReferrals.Referrer_Last_Name__c = 'TestLastName';
        newReferrals.Email__c = 'testreferrer@test.com';
        newReferrals.State__c = 'ca';
        insert newReferrals;
        KFGCreateTrainingReferralController controller=new KFGCreateTrainingReferralController();
        controller.save();
    }
}

Let us know if it helps.
This was selected as the best answer
Chiho SullivanChiho Sullivan
Thank you!  You saved me!