You need to sign in to do that
Don't have an account?
Tina 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.
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); } }
use below test class
100 % code coverage
All Answers
use below test class
100 % code coverage