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
Nathan Prats 22Nathan Prats 22 

Test Class for a Schedulable Apex Class

Hi !

I created a Schedulable Apex Class to reassign opportunities to the account owner every morning at 6 AM. 
global class ReassignOpps implements Schedulable {
    
    global void execute(SchedulableContext ctx) {
        List<Opportunity> OppList = [SELECT Id,OwnerId,Account.OwnerId 
                                     FROM Opportunity 
                                     WHERE Opportunity_Owner_Account_Owner__c = TRUE 
                                     AND IsClosed = FALSE 
                                     AND IsExcludedFromTerritory2Filter = FALSE];
        
        for(Opportunity Opp : OppList){    
            Opp.OwnerId = Opp.Account.OwnerId ;
        }
        
        update OppList;
    }
    
}
I'm fairly new to Apex, but I think I need to create a Test Class for this Class. 

I started to code this Test Class but I'm stuck. I have no idea what to do from here. 
@isTest
private class ReassignOppsTest {
    
    //Create 2 users
    User u1 = TestUtils.CreateAdminUser('user1');
    User u2 = TestUtils.CreateAdminUser('user2');
    
    //Create an account owned by X and an opportunity owned by Y
    Account acct    = TestUtils.CreateAccount('name');
    Account pacct   = TestUtils.CreatePartnerAccount('Partner name'+' partner');
    Contact con     = TestUtils.CreateContact('firstName','lastName',acct);
    Opportunity opp = TestUtils.CreateOpportunity(acct, pacct, con,'CA');
    
    acct.OwnerId = u1.Id;
    opp.OwnerID = u2.Id;
    
    //Call ReassignOpp Class???
    
    
    // test
    
    
}
I know that I need to create 2 users, 1 Account and 1 Opportunity, then launch the class somehow and then see if the opportunity has been reassign correctly. 

Any help would be more than welcome. 

Nathan
 
v varaprasadv varaprasad
Hi Nathan,

Please check once following sample code.

 
@isTest
private class ReassignOpps_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Account account_Obj = new Account(Name = 'Name331', DeleteOpp__c = false);
    Insert account_Obj; 
    Opportunity opportunity_Obj = new Opportunity(IsPrivate = false, Name = 'Name447',Opportunity_Owner_Account_Owner__c = True,IsExcludedFromTerritory2Filter = false,StageName = 'Prospecting', CloseDate = Date.today(),accountid = account_Obj.id);
    Insert opportunity_Obj; 
    test.stopTest();
  }
  
  static testMethod void test_execute_UseCase1(){
    List<Account> account_Obj  =  [SELECT Id,Name from Account];
    System.assertEquals(true,account_Obj.size()>0);
    List<Opportunity> opportunity_Obj  =  [SELECT Id,Name,IsClosed,OwnerId,Account.OwnerId from Opportunity];
    System.assertEquals(true,opportunity_Obj.size()>0);
    ReassignOpps obj01 = new ReassignOpps();      
    String sch = '0  00 1 3 * ?';
    system.schedule('Test', sch, obj01);
  }
}

Hope this helps you.

Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com​