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
salesforcerrrsalesforcerrr 

Simple Test Class for SOQL Query in Apex

Hi,  

would need assistance for getting started with testing apex classes. I basically created a simple class that is just a SOQL query returning a list of accounts owned by a given user: 
global with sharing class AccountRemoter {
    @RemoteAction
    global static List<Account> findAll() {
        return [SELECT Id, Owner.Name, Name, Phone, Email__c, ShippingLatitude, ShippingLongitude 
                    FROM ACCOUNT
               		WHERE OwnerId=:UserInfo.getUserID()];
		}
}

so far so good and all working well in sandbox but after searching and trying to find just some simple test class in order to achieve code coverage... the best i end up with is something like: 
 
@isTest
class AccountsListControllerTest {
	static testMethod void testAccountList (){
    List<Account> accounts = new List<Account>();
    for (Integer count = 0; count < 50; count++) {
        accounts.add(new Account (Name = 'Jack'+count, Phone ='00000000'+count));
    }
        
    insert accounts;
    AccountRemoter cc = new AccountRemoter(new ApexPages.StandardController(new Account()));
    Test.startTest();
        cc.findAll();
    Test.startTest();
}
}

which still gives me two problems: 
1. Constructor not defined: [AccountRemoter].<Constructor>(ApexPages.StandardController)
2. I think I would need some asserts below but i really cannot find a post where there is a simple test class 

Could someone guide me in the redirection and if this has been answered before please guide me in the right direction. After hours I am still unable to locate a solution. 

Thanks 
Best Answer chosen by salesforcerrr
Anurag SaxenaAnurag Saxena
Hi,
use this 
@isTest
class AccountsListControllerTest {
    static testMethod void testAccountList (){
    List<Account> accounts = new List<Account>();
    for (Integer count = 0; count < 50; count++) {
        accounts.add(new Account (Name = 'Jack'+count, Phone ='00000000'+count));
    }
        
    insert accounts;
   
    Test.startTest();
        AccountRemoter.findAll();
    Test.stopTest();
    Account ACC = [select phone from Account LIMIT 1];
        System.assertEquals ( Integer.valueOf(acc.phone) ,00000000);
    
}
}

Please choose as best answer

Thanks

All Answers

Anurag SaxenaAnurag Saxena
Hi,
use this 
@isTest
class AccountsListControllerTest {
    static testMethod void testAccountList (){
    List<Account> accounts = new List<Account>();
    for (Integer count = 0; count < 50; count++) {
        accounts.add(new Account (Name = 'Jack'+count, Phone ='00000000'+count));
    }
        
    insert accounts;
   
    Test.startTest();
        AccountRemoter.findAll();
    Test.stopTest();
    Account ACC = [select phone from Account LIMIT 1];
        System.assertEquals ( Integer.valueOf(acc.phone) ,00000000);
    
}
}

Please choose as best answer

Thanks
This was selected as the best answer
Anurag SaxenaAnurag Saxena

welcome
please choose as best answer