How To Write Test Class | Basics | Salesforce

How to write test classes in salesforce with basics

TEST CLASS BASICS

In this episode, we will talk about all the salesforce basics of an apex Test class.
Before starting anything we have to first understand why we have to write salesforce test class.

So let's get started kid...

When You Will Write Test Classes ? 
* If you want to validate whether the application what we have created is working as expected.

There are two ways of testing an application.
1. Using Salesforce User Interface
2. Testing bulk functionality (Upto 200 records can be passed through your code)

So before you deploy your code (to production)or package (managed) to the Force.com AppExchange the following conditions should be satisfied.

1) At least 75% of your Apex code must be covered by the unit test.
2) All the test cases must be covered successfully.

NOTE : When you deploy any code into production all the Test Methods will be executed and the test classes are not part of Apex code coverage.

Points to be noticed :
- Every Trigger must have a test case.
- All apex classes and triggers should compile successfully. 

What are the Factors that Need to be Tested in Apex Programming :

1. Single Action : This is to test a single record, produces the correct expected result.

2. Bulk Actions :  Any Apex code, whether a trigger, a class or an extension, may be invoked for 1 to 200 records. You must test not only the single record case but the bulk cases as well.

3. Positive Behaviour : Test to verify that the expected behaviour occurs through every expected permutation, that is the user filled out everything correctly and did not go past the limits.

4. Negative Behaviour : There are likely to your applications, such as not being able to add a future date, not being able to specify a negative amount & So on.

5. Restricted User : Whether a user with restricted access used in your code.

Apex Unit Test :
A unit test is the class methods that verify whether a particular piece of code is working properly or not.

Unit test method takes no arguments and commits no data to the database, will not send any emails.

Test Method Keyword :

- When you want to create a test method should be prefixed with the keyword testmethod.
- All the test methods are static.

For Example :
===========================================
public class myClass
{
 static testMethod void myTest()
 {
  //Add test method logic using system.assert(), system.assertEquals()
 //And system.assertNotEquals() here  
 }
}
===========================================

NOTE : testmethods cannot be defined in the Apex Trigger.

@isTest Annotation :

- If you define any method as @isTest then the method is the test method.
- If any class is defined with annotation @isTest then that class is defined as test class.

Example 1 :
===========================================
@isTest
private class myclass
{
 static testMethod void myTest()
 {
   //code_block
 }
}
===========================================

This is the same test class as in the previous example but it defines that test method with the isTest annotation instead.

Example 2 :
===========================================
@isTest
private class myclass
{
 @isTest static void myTest()
 {
  //code_block
 }
}
===========================================

NOTE: Class defined with the isTest annotation don't count against your organization limit of 3MB for all apex code.

Example 3 :
===========================================
@isTest
private class MyTestClass
{
 //Methods for testing
 @isTest static void test1()
 {
 //Implement test code
 }
 @isTest static void test2()
 {
  //Implement test code
 }
}
==========================================

- Test classes must be defined as @isTest with access specifies as public/private.
- ByDefault access level is private.
- Methods of the test class can be called only from the running method.
- A test method cannot be invoked in the non-test method.

So these are the basic fundamental things you must know before writing any apex test classes in salesforce ☁️⚡️

I hope you now it's quite easy for you to write salesforce test classes because now you know when and why exactly we write salesforce apex test classes.

In the next episodes, we will discuss and learn how to write test classes for all the type of your code wherever it is necessary. In a very easy way because when your purpose of writing code is clear you are always good to go. 

WOHOOO !! YOU HAVE JUST COMPLETED SALESFORCE TEST CLASS BASICS EPISODE
If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you 
Happy learning ☁️⚡️ (Learn. Help. Share.)
How To Write Test Class | Basics | Salesforce How To Write Test Class | Basics | Salesforce Reviewed by on Rating: 5

No comments:

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.