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
Madevala JithenderMadevala Jithender 

How do i write a Test class

public class AccountUtility {
    public static void ViewAnnualRevenue(){
        list<account> accountlist = [select id,name,annualrevenue from account];
        for(account acc : accountlist){
            string acctrev = acc.name + ':' + acc.annualrevenue;
            system.debug('acctrev'+acctrev);
        }
    }

}
Best Answer chosen by Madevala Jithender
SwethaSwetha (Salesforce Developers) 
HI Madevala,
Try the below class. It gives 100% coverage
@isTest
public class CreateaccntTest
{
    @isTest
    public static void CreateaccntMethod()
    {
        Account objAccount = new Account();
        objAccount.Name = 'Test Acc';
        insert objAccount;


	AccountUtility.ViewAnnualRevenue();
    }
}

If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Madevala,
Try the below class. It gives 100% coverage
@isTest
public class CreateaccntTest
{
    @isTest
    public static void CreateaccntMethod()
    {
        Account objAccount = new Account();
        objAccount.Name = 'Test Acc';
        insert objAccount;


	AccountUtility.ViewAnnualRevenue();
    }
}

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
mukesh guptamukesh gupta
Hi Madevala,

Please use below code that's cover all salesforce best practices for test class:-
 
@Test
public class AccountUtilityTest{
@TestSetup static void createRecords(){
	Account acc = new Account();
	acc.Name  = 'test acc';
	acc.annualrevenue  = 5000;
	insert acc;
	
}

testMethod static void testAccunt(){
 Account acc = [Select Id, Name, annualrevenue from Account where Name = 'test acc' LIMIT 1 ];
 system.assertEquals('test acc',acc.Name);
 AccountUtility.ViewAnnualRevenue();
}

}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh