Carry out the following steps to create an Apex test class, that provides unit test coverage for our Apex Controller, using the Salesforce Developer Console:
- Click on the Setup gear icon, as shown in the following screenshot:
The Setup gear icon is located in the top right-hand area of the main Home page.
- Click the Developer Console option, as shown in the following screenshot:
- In the resulting Developer Console window, click on File.
- Click on New.
- Click on Apex Class, as shown in the following screenshot:
- In the resulting New Apex class dialog, enter AccountSearchControllerTest in the Please enter a name for your new Apex class textbox.
- Click on OK, as shown in the following screenshot:
- In the resulting Apex class edit page, paste the following code:
@isTest private class AccountSearchControllerTest{ @isTest static void test_getAccounts(){ // Create test data List<Account> lstAccTestData = new List<Account>{}; // Create test data for(Integer i = 5; i < 10; i++){ Account acc = new Account(Name = 'Test Account ' + i); lstAccTestData .add(acc); } Test.startTest(); List<Account> lstAccResult = new List<Account>{}; // Test Account Does Not exist String sNonExistingAccount = 'Non Existing Account'; lstAccResult =
AccountSearchController.getAccounts(sNonExistingAccount); System.assertEquals(null, lstAccResult); // Test Account Does Exist String sDoesExistAccount = 'Test Account'; lstAccResult =
AccountSearchController.getAccounts(sDoesExistAccount); System.assertNotEquals(null, lstAccResult); Test.stopTest(); } }
- Click on File.
- Click on Save, as shown in the following screenshot: