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
Ola BamideleOla Bamidele 

How to write Test Class for Apex Code that displays fields

Hi Everyone, 

I have writen a short apex code to be able display a couple of fields from Salesforce however, I now have to write a test class, 

So it would be great if someone could please assist me :)  

This is my short apex code that I have:
public with sharing class AccountController {
    @AuraEnabled
    public static List<Account> findAll() {
    return [SELECT id, name, Location__Latitude__s, Location__Longitude__s, Industry
            FROM Account
            WHERE Location__Latitude__s != NULL AND Location__Longitude__s != NULL
            LIMIT 50];
    }
    
}

 
Best Answer chosen by Ola Bamidele
Amit Chaudhary 8Amit Chaudhary 8
Try to update code like below
@isTest 
public class AccountControllerTest 
{
    static testMethod void testMethod1() 
	{
		Account acc = new Account();
		acc.Name='Test';
		//acc.Location__Latitude__s   =22.2222;
		//acc.Location__Longitude__s  =22.2222;
		insert acc;

		List<Account> lstAcc = AccountController.findAll();
		//System.assert(lstAcc.size() > 0 );
		
		
    }
}

Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


You write a test class for this the same way that you would any other:
- Set up some data for the Apex to access
- Verify the behaviour with asserts.

Try below sample test class.
 
@isTest 
public class AccountControllerTest 
{
    static testMethod void testMethod1() 
	{
		List<Account> lstAcc = new List<Account>();

		Account acc = new Account();
		acc.Name='Test';
		//acc.Location__Latitude__s   =22.2222;
		//acc.Location__Longitude__s  =22.2222;
		insert acc;

		List<Account> lstAcc = AccountController.findAll();
		//System.assert(lstAcc.size() > 0 );
		
		
    }
}

Let us know if this will help you
 
Ola BamideleOla Bamidele
Hi Amit Chaudhary 8,

Thanks for your response. However i tried teh code and I keep getting a "Duplicate variable: lstAcc" error. 

Do you know what its means? What does ISAcc mean? 

Thanks very much!
Amit Chaudhary 8Amit Chaudhary 8
Try to update code like below
@isTest 
public class AccountControllerTest 
{
    static testMethod void testMethod1() 
	{
		Account acc = new Account();
		acc.Name='Test';
		//acc.Location__Latitude__s   =22.2222;
		//acc.Location__Longitude__s  =22.2222;
		insert acc;

		List<Account> lstAcc = AccountController.findAll();
		//System.assert(lstAcc.size() > 0 );
		
		
    }
}

Let us know if this will help you
This was selected as the best answer
Ola BamideleOla Bamidele
Hi Amit Chaudhary 8, 

I tried the code and its give 100% coverage so thanks! However, I have a question. 

I noticed that the Test Class only covers these two lines:
 
public static List<Account> findAll() {
    return [SELECT id, name, Location__Latitude__s, Location__Longitude__s, Industry

And these lines arent covered:
 
FROM Account
            WHERE Location__Latitude__s != NULL AND Location__Longitude__s != NULL
            LIMIT 50];
    }
    
}

Is there a reason for this? Are these lines not suppose to be covered? Thanks very much!
Amit Chaudhary 8Amit Chaudhary 8
Hi Ola Bamidele,

Code coverage is 100% means all lines are covered.

Full Select Query will consider as one line only. I hope none of the line is coming in red colour.
 
Ola BamideleOla Bamidele
Hi Amit Chaudhary 8, 

No, none of the lines are appearing in red, so its all covered then.


Thanks very much!