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
rotfilrotfil 

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.
 
@isTest
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
		Test.startTest();
		PageReference pageRef = Page.yourPageName; //put your vf page name here
        Test.setCurrentPage(pageRef);
        Apexpages.currentpage().getparameters().put('name','yourTestAccount');
        ApexPages.currentPage().getParameters().put('type','yes');
		
		yourClassName testClass = new yourClassName(); //put your controller class name here
		testClass.getJsonListOfAccounts ();
		Test.stopTest();
	}
}

Hope this helps.

Ashish Kr.