Master the concepts of Salesforce in just 45 days Click here to know more.
8
10
58
27
Premium Salesforce Weekend Batch Start From Tomorrow.
Unit tests are class methods that verify whether a particular piece of code is working properly or not.
Unit test methods take no arguments, commit no data to the database, send no emails, and are flagged with the testMethod keyword or the isTest annotation in the method definition.
Also, test methods must be defined in test classes , that is, classes annotated with isTest.
Create different different test methods to test different functionalities.
In each test method write different test cases to test your code whether it is working properly with the different inputs or not.After API version 32 test method is used as @istest.
Ex:
@isTest
Private class myClass {
Static void testMethod myTest {
// code block
}
static void myTest() {
Use the isTest notation to define classes and methods that only contain code used for testing your application. The isTest annotation on methods is equivalent to the testMethod keyword.
Note: Classes defined with the isTest annotation don’t count against your organization limit of 3 MB for all Apex code.
This is an example of a test class that contains two test methods:
Private class MyTestClass {
// Methods for testing
@isTest static void test1() {
// Implement test code
@isTest static void test2() {
Some characteristics of unit test methods are that they:
Here are some things to note about unit tests.
Accessing Private Test Class Members:
If class Members are private, they aren’t visible to the test class.
For Example:
Public class VisibleSampleClass {
// Private member variables
@TestVisible private integer recordNumber = 0;
@TestVisible private string areaCode = ‘(415)’;
// Public member Variable
Public integer maxRecords = 1000;
// Private inner class
@TestVisible class Employee {
String fullName;
String phone;
// Constructor
@TestVisible Employee(String s, String ph) {
fullName = s;
Phone = ph;
// Private method
@TestVisible private string privateMethod(Employee e) {
System.debug(‘I am private’);
recordNumber++;
String phone = areCode + ‘ ’ + e.phone;
String s = e.fullName + ‘\’s phone number is’+ phone;
System.debug(s);
return s;
// Public method
Public void public method() {
maxRecords++;
System.debug(‘I am public’);
// Private custom exception class
@TestVisible private class MyException extends Exception {}
Note: Test methods aren’t allowed in non-test classes, you must move the test methods from the old class into a new test class (a class annotated with isTest) when you upgrade the api version of your class.
You might run into visibility issues when accessing private methods or member variables of the original class.
In this case, just annotate these private members with TestVisible.
< < Previous
Next > >
The batch is going to start shortly. Fill the form given below to Register yourself now.
Fill out the form and get consulted by our Salesforce experts.
Fill out the form to figure out to detemine the perfect Salesforce profile for yourself.
Fill the form below to get a demo of this course.
Fill out the form to get your Premium Salesforce Development Course
Online Salesforce Development Course is soon going to be launched. Please fill the form and we will notify you about the course.