DEV Community

loading...

Apex Testing

bhanukarkra
Mechanical Engineer who is learning Code
Updated on ・1 min read

Syntax

@isTest static void testName() {
// code_block
}

or

static testMethod void testName() {
// code_block
}

Code Coverage: common cause of lower coverage is because code is not covering all data values for conditional code execution

Exception Handling
Built in Exception:
*DmlException: Any problem with a DML statement, such as an insert statement missing a required field on a record.

*ListException:Any problem with a list, such as attempting to access an index that is out of bounds.

*NullPointerException: Any problem with dereferencing a null variable.

*QueryException:Any problem with SOQL queries, such as assigning a query that returns no records or more than one record to a singleton sObject variable.

*SObjectException: Any problem with sObject records, such as attempting to change a field in an update statement that can only be changed during insert.

Common Handling Methods:
*getCause: Returns the cause of the exception as an exception object.
*getLineNumber: Returns the line number from where the exception was thrown.
*getMessage: Returns the error message that displays for the user.
*getStackTraceString: Returns the stack trace as a string.
*getTypeName: Returns the type of exception, such as DmlException, *ListException, MathException, and so on.
*getDmlFieldNames(Index of the failed record): Returns the names of the fields that caused the error for the specified failed record.
*getDmlId(Index of the failed record): Returns the ID of the failed record that caused the error for the specified failed record.
*getDmlMessage(Index of the failed record): Returns the error message for the specified failed record.
*getNumDml: Returns the number of failed records.

Discussion (0)

Forem Open with the Forem app