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
Eleonora PeruffoEleonora Peruffo 

Apex test method for a list

Hi All,

I'm new to apex coding and I'm banging my head on the wall to write a test class and be able deploy my apex class but with no result yet. The aim is to display a list of accounts which respect certain criteria- being an active member and being a small organisation. I've used my class in a page and it seems to work fine in the sandbox.
I've looked at examples of test classes but none seems to work. Any good samaritan out there who can give me a hand?

This is my class:

public with sharing class Small_members {
List<Account> accounts;
public List<Account> getAccounts(){
accounts = [SELECT name, Phone, County__c, Website, Category_ID__c from account WHERE (OrgMembership_Status__c = 'Active' and Category_ID__c='Small Organisation') ORDER BY Name];
return accounts;
}
}

and this is my test class so far - not working :-(

@isTest
public class Testmallmembers { static testMethod void t1()
{ account a = new Account( name='accountmember1', Phone='045-555555', County__c='Countysoandso', Website='http://www.website.ie') ;
insert a;
Testmallmembers Small_members = new Small_members();
List<Contact> l = Small_members.getAccounts();
system.assert( l.size() > 0 ); }
public List<Account> 'accountmember1';
                           }

Any tips on how to proceed would be appreciated. Thank you.
Best Answer chosen by Eleonora Peruffo
dev_sfdc1dev_sfdc1
Hi Eleonora PeruffoEleonora Peruffo,

Try this may help you getting 100%:

@isTest
private class TestAccToSave{
static testmethod void myTest(){
account a = new Account( name='accountmember1',Website='http://www.website.ie') ;
insert a;

  Small_members ats = new Small_members();
  ats.getAccounts();

 
}
}

All Answers

Ashish_SFDCAshish_SFDC
Hi, 


Test classes are "See All Data = false " by default and the existing records will not be called as they are not visible to the test class. 

The best practice is not to use the Original Production Data but to insert some dummy records in the test class itself and then try to call them. 

But for few complex scenarios we have to write some test classes where we would need real time data there we can mark - SeeAllData = TRUE . 

See the links below for similar threads and sample code. 

http://salesforce.stackexchange.com/questions/10168/test-methods-for-the-soql-in-the-extension-class

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008xY2IAI

 https://developer.salesforce.com/forums/ForumsMain?id=906F000000091AyIAI


Regards,
Ashish
dev_sfdc1dev_sfdc1
Hi Eleonora PeruffoEleonora Peruffo,

Try this may help you getting 100%:

@isTest
private class TestAccToSave{
static testmethod void myTest(){
account a = new Account( name='accountmember1',Website='http://www.website.ie') ;
insert a;

  Small_members ats = new Small_members();
  ats.getAccounts();

 
}
}
This was selected as the best answer
Eleonora PeruffoEleonora Peruffo
Thank you to you both for the very helpful info! Made my day! :-)