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
IR_Admin IR_SalesforceIR_Admin IR_Salesforce 

I need to write a test class for apex class to update all Opportunities

I think I have an Apex Class that will update all Opportunities at night so that the workflows will run on them on a nightly basis. 

public class updateOpportunity {
    public static void checkUpdate(Opportunity[] objects){
        for(Opportunity obj: objects){
       
        }
    }
}

I need to write a test class so that I can move this over into production. Can someone please help me?
 
Best Answer chosen by IR_Admin IR_Salesforce
Raj VakatiRaj Vakati
try this pls 
 
@isTest
public class TestupdateOpportunity {
    static testMethod void insertNewOpportunity() {
    Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity Triggers',  
          StageName = 'Sourcing Demand'
       );
       insert testOpportunity;
        Test.startTest();
        Opportunity[] objects = new Opportunity[] {testOpportunity };
		
      updateOpportunity.checkUpdate(objects);
        Test.stopTest();
    }
}

 

All Answers

Raj VakatiRaj Vakati
@isTest
public class TestupdateOpportunity {
    static testMethod void insertNewOpportunity() {
       
        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity Triggers',     
            StageName = 'Sourcing Demand'
        );
        insert testOpportunity;

       
        Test.startTest();
		Opportunity[] objects = new Opportunity[] ; 
		objects.add(testOpportunity) ;

        updateOpportunity.checkUpdate( objects);

        Test.stopTest();

        );
    }
}

 
IR_Admin IR_SalesforceIR_Admin IR_Salesforce
Hi @Raj V

I've got it done to this:

@isTest
public class TestupdateOpportunity {
    static testMethod void insertNewOpportunity() {
    Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity Triggers',  
          StageName = 'Sourcing Demand'
       );
       insert testOpportunity;
        Test.startTest();
        Opportunity[] objects = new Opportunity[] {
        objects.add(testOpportunity) };
      updateOpportunity.checkUpdate( objects);
        Test.stopTest();
    }
}

But am now getting this error : 

Error: Compile Error: Variable does not exist: objects at line 11 column 9
 
Raj VakatiRaj Vakati
try this pls 
 
@isTest
public class TestupdateOpportunity {
    static testMethod void insertNewOpportunity() {
    Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity Triggers',  
          StageName = 'Sourcing Demand'
       );
       insert testOpportunity;
        Test.startTest();
        Opportunity[] objects = new Opportunity[] {testOpportunity };
		
      updateOpportunity.checkUpdate(objects);
        Test.stopTest();
    }
}

 
This was selected as the best answer
IR_Admin IR_SalesforceIR_Admin IR_Salesforce
Thanks Raj V that worked! 
I tried to do the same for updating the Opportunity line item but seem to be having some trouble.
Apex Class:
public class UpdateOpportunityLineItem {
    public static void checkUpdate(OpportunityLineItem[] objects){
        for(OpportunityLineItem obj: objects){
       
        }
    }
}

@isTest
public class TestupdateOpportunityLineItem {
    static testMethod void insertNewOpportunityLineItem() {
    OpportunityLineItem testOpportunityLineItem = new OpportunityLineItem(
            Type__c = 'New', 
          Position_Status__c = 'Recruiting to Confirm',
OpportunityId = opp.Id, 
Start_Date__c = Date.newInstance(2016, 12, 9), 
Disciplines__c = 'JavaScript Engineering'

       );
       insert testOpportunityLineItem;
        Test.startTest();
        OpportunityLineItem[] objects = new OpportunityLineItem[] {testOpportunityLineItem };
      updateOpportunityLineItem.checkUpdate(objects);
        Test.stopTest();
    }
}

I'm unsure what to enter for OpportunityId = opp.Id?