How to do it...

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:

  1. 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.

  1. Click the Developer Console option, as shown in the following screenshot:
  1. In the resulting Developer Console window, click on File.
  2. Click on New.
  3. Click on Apex Class, as shown in the following screenshot:
  1. In the resulting New Apex class dialog, enter AccountSearchControllerTest in the Please enter a name for your new Apex class textbox.
  2. Click on OK, as shown in the following screenshot:
  1. 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(); } }
  1. Click on File.
  2. Click on Save, as shown in the following screenshot:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset