You need to sign in to do that
Don't have an account?

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.
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.
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
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
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();
}
}