+ Start a Discussion
Tina Chang 6Tina Chang 6 

How to Write a Test Class for SOQL and Account Field Update?

Hello, I've just started to learn about Apex and I'm having difficulty writing a test class for the following Apex class. 

The following Apex class has code to retrieve Account records and logic to iterate over a list of Account records and update the Description field.  I learned this Apex class through Trailhead and wanted to write a test class for it but couldn't figure out how.
public class OlderAccountsUtility {
    public static void updateOlderAccounts() {
      // Get the 5 oldest accounts
      Account[] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
      // loop through them and update the Description field
      for (Account acct : oldAccounts) {
          acct.Description = 'Heritage Account';
      }
      // save the change you made
      update oldAccounts;
    }
}
This is what I have written.  Can anyone show me how to fix this Apex test unit so it works?  Any help would be much appreciated!
@isTest
private class OlderAccountsUtilityTest {
    
    @isTest static void createAccount() {
    
    List <Account> accts = new List<Account>();
      for(integer i = 0; i<200; i++) {
         Account a = new Account(Name='testAccount'+'i');
         accts.add(a);
    }
        
   	insert accts;

    Test.StartTest();	
        
        List<Account> accList = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
     
 		for (Account acct : accList) {
        acct.Description = 'Heritage Account';
        }

    Test.StopTest();

	System.AssertEquals(
    
    	database.countquery('SELECT COUNT() FROM Account WHERE Description' = 'Heritage Account'), 5);
             
    }
}

 
Best Answer chosen by Tina Chang 6
karthikeyan perumalkarthikeyan perumal
Hello Tina

use below test class 

100 % code coverage
 
@isTest
private class OlderAccountsUtilityTest {
    
    @isTest static void createAccount() {
    
    Account accts = new Account();
      
    accts.Name = 'testAccount';
         
    Insert accts;
    
    OlderAccountsUtility.updateOlderAccounts();       
    
  }
     
}

 

All Answers

Tina Chang 6Tina Chang 6
Hi, Vinod! Thanks a lot for your swift reply!  I tried and got the following errors.  These errors are over my head...  Perhaps you will have some clues?

Apex Errors
Vinod ChoudharyVinod Choudhary
Opps.. sorry Tine
 
@isTest
private class OlderAccountsUtilityTest {
    
    @isTest static void createAccount() {
    
    Test.StartTest();   
    Account accts = new Account();
      // Write mandatory fields 
    accts.Name = 'testAccount';
        
    Upsert accts;

    Test.StopTest();

    System.AssertEquals(
    
        //database.countquery('SELECT COUNT() FROM Account WHERE Description' = 'Heritage Account'), 5);
             
    }
}

 
karthikeyan perumalkarthikeyan perumal
Hello Tina

use below test class 

100 % code coverage
 
@isTest
private class OlderAccountsUtilityTest {
    
    @isTest static void createAccount() {
    
    Account accts = new Account();
      
    accts.Name = 'testAccount';
         
    Insert accts;
    
    OlderAccountsUtility.updateOlderAccounts();       
    
  }
     
}

 
This was selected as the best answer
sfdcMonkey.comsfdcMonkey.com
HI use below test class for 100% coverge :
@isTest
private class OlderAccountsUtilityTest {
    
    @isTest static void createAccount() {
    
    Test.StartTest();   
    List<Account> accts = new List<Account>();
        for(integer i = 0; i<10; i++) {
            Account a = new Account();
             a.Name='testAccount '+ i ;
            // add all required fields here for account
            accts.add(a);
        }
     
   	insert accts;

   OlderAccountsUtility.updateOlderAccounts(); 

	
        Test.StopTest();       
    }
}
Thanks , let us know if it helps you

 
Tina Chang 6Tina Chang 6
Thank you so much, @Vinod and @karthikeyan!  I had been overthinking and am grateful for your help/answers!
Tina Chang 6Tina Chang 6
@piyush_soni, your test class works like a charm, too. Thank you so much for your help!