Be a champ in test class. Test classes from start to end.

For development of robust, error-free code, Apex Code requires the creation and execution of unit tests.Unit tests are class methods that verify whether a particular piece of code is working properly.




 Unit tests are written in Apex Code and annotated with the  annotation @testmethod. The test methods must provide at least 75% code coverage. Code coverage is calculated by dividing the number of unique Apex code lines executed during your test method execution by the total number of Apex code lines in all of your trigger and classes.

Defining a Test Method using the testMethod keyword

For defining  an Apex method as a 'test method',Define the method as static and add the keyword testmethod. 
Classes defined with the isTest annotation do not count against your organization limit of 2 MB for all Apex code. Classes annotated with isTest can be declared as private or public. 

@isTest
public class totestClass {
    static testMethod void myTest() {
      
     }
}

Test.startTest/Test.stopTest

These methods, Test.startTestand Test.stopTest, are used when testing governor limits. Or in other words, executing test scenarios with a larger data set.
static testMethod void testTeststartandEnd(){
    // Perform our data preparation.
    List<Account> accounts = new List<Account>{};
        
    for(Integer i = 0; i < 200; i++){
        Account a = new Account(Name = 'limit Account ' + i);
        accounts.add(a);
    }

    // Start the test, this changes governor limit context to 
    // that of trigger rather than test. 
    test.startTest();
  
    // Insert the Account records that cause the trigger to execute.
    insert accounts; 
  
    // Stop the test, this changes limit context back to test from trigger.
    test.stopTest();
  
    // Query the database for the newly inserted records.
    List<Account> insertedAccounts = [SELECT Name, Description 
                                      FROM Account 
                                      WHERE Id IN :accounts];
  
    // Assert that the Description fields contains the proper value now.
    for(Account a : insertedAccounts){
      System.assertEquals(
        'This Account is probably left over from testing. It should probably be deleted.', 
        a.Description);
    }
}

System.runAs()

System.runAs(), lets you write test methods that change user contexts to either an existing user or a new user. All of that user's record sharing is then enforced. 
System.assert()
  • System.Assert accepts two parameters, one (mandatory) which is the condition to test for and the other a message (optional) to display should that condition be false.
  • System.AssertEquals and System.AssertNotEquals both accepts three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false.
public class assertClass {     public void insertAcct(String name, String extId) {         Account acc = new Account(Name = name,External_ID__c = externltId);         insert acc;         System.assertEquals('Test',acc.Name);     } }

HttpCalloutMock For Apex Test Class

Check the below example nothing better then this :
https://developer.salesforce.com/blogs/developer-relations/2013/03/testing-apex-callouts-using-httpcalloutmock.html
http://www.joe-ferraro.com/2014/04/apex-unit-testing-test-data-and-test-setmock/

@TestSetup

Any DML statement within the context of @testSetup will not count against your overall test class limits but SOQL statements will.
@iSTest
public class setupTest {   

    
    @testsetup
    static void createLead(){
        List<Lead> lstLead = new List<Lead>();        
        for(Integer i = 0 ; i<200 ; i++) {
            lstLead.add(new Lead(lastName = 'setup'+i , Company = 'SF'));
        } 
        insert lstLead ;
    }
    
   //Check whether records created in test method
   //is accessible or not
    public static testMethod void test(){
        System.assertEquals(200, [SELECT COUNT() FROM Lead Where company = 'SF']);
    }
}

Resource : Salesforce documentation

Comments

Popular posts from this blog

All about workflow rule , limitation and important points with example

All about Service Oriented Architecture : SOAP / REST differences and usage

Custom Setting in Salesforce