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
GregNYCGregNYC 

Apex Test Coverage Assistance

Hello,


I'm new to Apex development, and while my class and visualforce page are working nicely, I'm a bit at a loss for getting Test Coverage working.

 

My class:

 

 

public class FlowReference {

      private final Program__c program;

      public FlowReference() {
            program = [select id, name, Product_Summary__c, Position1_Item__r.Name, Position2_Item__r.Name, Position1_Item__r.FAQ__c, Position2_Item__r.FAQ__c, Customer_Service_Info__c from Program__c where id =
                       :ApexPages.currentPage().getParameters().get('id')];
      }

      public Program__c getProgram() {
            return program;
      }
      
      //TEST COVERAGE
      public static testMethod void testFlowReference() {
          //Create program
          Program__c p = new Program__c(name='Test Program');
          insert p;
      
      p.CampaignID__c = '123';
      update p;
      
      FlowReference class1 = new FlowReference();
      class1.name = p.CampaignID__c; // I know this is wrong!!
      
      Program__c updatedProgram = [SELECT CampaignID__c FROM Program__C WHERE Id = :p.Id];
      System.assertEquals('123', updatedProgram.CampaignID__c);
      
      }
}

 I know things go bad around class1.name = p.CampaignID__c...

 

Any help is appreciated!

Thanks.

 

MandyKoolMandyKool

Hi,

 

You can write your testMethod as follows:

 

 

public static testMethod testFlowReference()
    {
        @startTest
        
        //Create Position1_Item__c record
        //And add all required fields for that object.
        //This is the sample record that I am creating.
        Position1_Item__c objPos1 = new Position1_Item__c();
        objPos1.Name = 'TestPostion1';
        objPos1.FAQ__c = 'TestFAQ1';
        insert objPos1;
        
        //Create Position2_Item__c object
        //And add all required fields for that object.
        //This is the sample record that I am creating.
        Position2_Item__c objPos2 = new Position2_Item__c();
        objPos2.Name = 'TestPostion2';
        objPos2.FAQ__c = 'TestFAQ2';
        insert objPos2;
        
        //Insert Program__c record.
        //And add all required fields for that object also
        //the fields that you have used in your code.
        Program__c objProgram = new Program__c();
        objProgram.Name = 'TestProgram';
        objProgram.Product_Summary__c = 'TestProductSummary';
        objProgram.Position1_Item__c = objPos1.Id;
        objProgram.Position2_Item__c = objPos2.Id;
        objProgram.Customer_Service_Info__c = 'TestCustServiceInfo';
        insert objProgram;
        
        @stopTest
        
        //Now pass the id of this Program to your VF Page.
        PageReference pageRef = Page.YourVFPageName; //Add your VFPageName
        Test.setCurrentPage(pageRef); 
        ApexPages.currentPage().getParameters().put('id', objProgram.Id); // Passing Id to your page.
        
        //Now Call your controller
        FlowReference objFlowRef = new FlowReference();
    }

 

You may face some issues as I dont knwo the exact object model that you have.

 

Also as a best practice you should avoid writing the code in constructor of the controller as you will not be able to control the execution of the constructor instead you can use "action" attribute of VF Page which will be called on PageLoad.

 

Also there are lots of best practices  for TestMethods that you should follow, that will help you in writing proper test cases.  You can read more about Test Methods by going to following link

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

 

If you need further assistance, please feel free to post it here.