+ Start a Discussion
rotfilrotfil 

Test classes! How do I write a test class for a simple query class

How would I go about writing a test class for the following simplified class method?

    public static string getJsonListOfAccounts () {

        String searchName = Apexpages.currentPage().getParameters().get('name');  

        String searchType = Apexpages.currentPage().getParameters().get('type');

        List<Account> listOfAccounts = new List<Account>();
       
        if (searchType == 'yes') {

            jsonListOfAccounts = [SELECT Id, Name, SummitElse from Account where example__c =: 'yes' AND Name like : '%'+ searchString +'%'];

        } else {

            jsonListOfAccounts = [SELECT Id, Name from Account where example__c =: 'no' AND Name like : '%'+ searchString +'%'];

        }
       
        return JSON.serializePretty(jsonListOfAccounts);

    }
Best Answer chosen by rotfil
rotfilrotfil
Hi Ashish Kumar - yea, I posted it in the wrong section - the other question for future anyones refrence is here:

https://developer.salesforce.com/forums/ForumsMain?id=9060G000000Xa1pQAC

All Answers

rotfilrotfil
public static string getJsonListOfAccounts () {

        String searchName = Apexpages.currentPage().getParameters().get('name');  

        String searchType = Apexpages.currentPage().getParameters().get('type');

        List<Account> listOfAccounts = new List<Account>();
       
        if (searchType == 'yes') {

            jsonListOfAccounts = [SELECT Id, Name, SummitElse from Account where example__c =: 'yes' AND Name like : '%'+ searchString +'%']; 

        } else {

            jsonListOfAccounts = [SELECT Id, Name from Account where example__c =: 'no' AND Name like : '%'+ searchString +'%']; 

        }
       
        return JSON.serializePretty(jsonListOfAccounts);

    }

 
Ashish KumarAshish Kumar
Hi rotfil,

I guess its a duplicate post by you only. I have answered for the question on your another similar post. you can follow that.
Good luck!
rotfilrotfil
Hi Ashish Kumar - yea, I posted it in the wrong section - the other question for future anyones refrence is here:

https://developer.salesforce.com/forums/ForumsMain?id=9060G000000Xa1pQAC
This was selected as the best answer
Ashish KumarAshish Kumar
Yes. I have already replied to your question there. Please check and say cheers if that helped. :)