+ Start a Discussion

Writing a test class for this simple method

I am new to salesforce and given the documentation I am at a loss of where to start! If someone could write a test class for the following piece of code that would be most handy. I have simplified the method. I don't quite understand the 100% code coverage. I saw another post write something that didn't really make a whole bunch of sense.
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
Ashish KumarAshish Kumar
Hi rotfil,

you could start by following below code.
private class TestYourClassName {
    static testMethod void testJsonListOfAccount() {
		Account testAccount = new Account(Name = 'yourTestAccount',SummitElse ='testSummitElse',example__c='yes' );
		// Other required fields here
		insert testAccount;
		// Test code within
		PageReference pageRef = Page.yourPageName; //put your vf page name here
		yourClassName testClass = new yourClassName(); //put your controller class name here
		testClass.getJsonListOfAccounts ();

Hope this helps.

Ashish Kr.