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
John NeffJohn Neff 

How do I cover "UPDATE" in my test class?

Hello, 

I have a controller which includes serveral UPDATE statements - however I am running into a code coverage error and I think it is because my UPDATE is not covered in my test class.  I tried to add it but I ran into an error.  What is the best way for me to beef up the test class for the controller below?  Thanks in advance!
 
CLASS:

public class R2MPipeLineController{

    public List<Opportunity> listOfLive {get; set;}
    public List<Opportunity> listOfViability {get; set;}
    public List<Opportunity> listOfLaunchPad {get; set;}
    public List<Opportunity> listOfContractSent {get; set;}
    public List<Opportunity> listOfQualified {get; set;}
    public Opportunity Live {get; set;}
    public Opportunity Viability {get; set;}
    public Opportunity LaunchPad {get; set;}
    public Opportunity ContractSent {get; set;}
    public Opportunity Qualified {get; set;}
    
    public PageReference saveCS(){
    UPDATE listOfContractSent;
    return null;
    }
    
    public PageReference saveVI(){
    UPDATE listOfViability;
    return null;
    }
    
    public PageReference saveLP(){
    UPDATE listOfLaunchPad;
    return null;
    }
    
    public PageReference saveQD(){
    UPDATE listOfQualified;
    return null;
    }
    
    public String getName(){
        return 'R2MPipeLineController';
        }
    
public R2MPipeLineController() {
    listofLive = [Select id, name, CreatedDate, Company_Name__c, Next_Step__c, Vertical__c, Weeks_Live__c, Days_Since_Last_Modified__c, Contract_SENT__c, NextStep, LeadSource, Probability, Spend_Last_Week__c, Spend_Last_30_Days__c, Owner_Name__c, Revenue_All_Time__c from Opportunity WHERE StageName = 'Live' ORDER BY Weeks_Live__c DESC];
    listOfViability = [Select id, name, Amount, Projected_Revenue__c, CreatedDate, Company_Name__c, Next_Step__c, Vertical__c, Weeks_Live__c, Days_Since_Last_Modified__c, Contract_SENT__c, NextStep, LeadSource, Probability, Spend_Last_Week__c, Spend_Last_30_Days__c, Owner_Name__c, Revenue_All_Time__c from Opportunity WHERE StageName = 'Viability' ORDER BY Days_Since_Last_Modified__c DESC];
    listOfLaunchPad = [Select id, name, Amount, Projected_Revenue__c, CreatedDate, Company_Name__c, Next_Step__c, Vertical__c, Weeks_Live__c, Days_Since_Last_Modified__c, Contract_SENT__c, NextStep, LeadSource, Probability, Spend_Last_Week__c, Spend_Last_30_Days__c, Owner_Name__c, Revenue_All_Time__c from Opportunity WHERE StageName = 'Launch Pad' ORDER BY Days_Since_Last_Modified__c DESC];
    listOfContractSent = [Select id, name, Amount, Projected_Revenue__c, CreatedDate, Company_Name__c, Next_Step__c, Vertical__c, Weeks_Live__c, Days_Since_Last_Modified__c, Contract_SENT__c, NextStep, LeadSource, Probability, Spend_Last_Week__c, Spend_Last_30_Days__c, Owner_Name__c, Revenue_All_Time__c, Contract_Age__c from Opportunity WHERE StageName = 'Contract Sent' ORDER BY Contract_Age__c ASC];
    listOfQualified = [Select id, name, Amount, Projected_Revenue__c, CreatedDate, Company_Name__c, Next_Step__c, Vertical__c, Weeks_Live__c, Days_Since_Last_Modified__c, Contract_SENT__c, NextStep, LeadSource, Probability, Spend_Last_Week__c, Spend_Last_30_Days__c, Owner_Name__c, Revenue_All_Time__c from Opportunity WHERE StageName = 'Qualified' ORDER BY Probability DESC]; 

}
}

And here is my test class: 
 
@isTest(seeAllData = false)
public class R2MPipeLineControllerTest{
    // Unit test Method
    static testmethod void UnitTest() {
        //Create your buyer record with required field
        //Opportunity o = new Opportunity(StageName = 'Qualified');
        //insert o;
        test.startTest();
           R2MPipeLineController qo = new R2MPipeLineController();
        test.stopTest();
    }   
}

 
Best Answer chosen by John Neff
Ray C. KaoRay C. Kao
Hi John

  Try this code
  
@isTest(seeAllData = false)
public class R2MPipeLineControllerTest{
    // Unit test Method
    static testmethod void UnitTest() {
        //Create your buyer record with required field
        //Opportunity o = new Opportunity(StageName = 'Qualified');
        //insert o;
        test.startTest();
           R2MPipeLineController r2mpl = new R2MPipeLineController();
           r2mpl.saveCS();
           r2mpl.saveVI();
           r2mpl.saveLP();
           r2mpl.saveQD();
           r2mpl.getName();
        test.stopTest();
    }   
}

 

All Answers

SonamSonam (Salesforce Developers) 
John,

You need to call the update methods in your test class and confirm the changes in case these methods are making any changes to the data.

Also, run this test class in the developer Console and this will give you a better idea as to which lines are not covered yet.
Ray C. KaoRay C. Kao
Hi John

  Try this code
  
@isTest(seeAllData = false)
public class R2MPipeLineControllerTest{
    // Unit test Method
    static testmethod void UnitTest() {
        //Create your buyer record with required field
        //Opportunity o = new Opportunity(StageName = 'Qualified');
        //insert o;
        test.startTest();
           R2MPipeLineController r2mpl = new R2MPipeLineController();
           r2mpl.saveCS();
           r2mpl.saveVI();
           r2mpl.saveLP();
           r2mpl.saveQD();
           r2mpl.getName();
        test.stopTest();
    }   
}

 
This was selected as the best answer