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

<< PREVIOUS                                         NEXT >>

How To Write Test Class | Basics | Salesforce How To Write Test Class | Basics | Salesforce Reviewed by on Rating: 5

20 comments:

  1. Hi, I was hoping you could give insight on how @AuraEnabled classes are to be tested such as when you are passing the recordid back to the component. I am trying to do an assertion when I start test but the list is returning null.

    ReplyDelete
    Replies
    1. Could you please share more details about it ?

      Delete
  2. Nice post. I was checking constantly this blog and I’m impressed! Extremely useful info specially the last part I care for such information a lot. I was seeking this certain info for a long time. Thank you and good luck.write for us

    ReplyDelete
  3. This is one of the new casinos gaming sites that intends to please its high roller clients. real casino online gambling

    ReplyDelete
  4. Fake health organizations. Scammers pose as health authorities like the World Health Organization (WHO) and U.S. make money from home

    ReplyDelete
  5. Koçak Vize Vize Hizmetleri Vize işlemleri Elçiliğe Gitmeden Vize Alma Online Vize Başvurusu İşlemleri Vize Danışmanlık Firması olarak hizmetinizdeyiz Almanya vizesi

    ReplyDelete

  6. Really impressive post. I read it whole and am going to share it with my social circles. I enjoyed your article and am planning to rewrite it on my own blog . how to get a united nations passport

    ReplyDelete
  7. There are even times when I feel so down but now I will feel better right after checking your blogs. You have made me feel so good about myself all the time and please know that I do appreciate everything that you have in your blog top gun leather jacket. I absolutely loved each little bit of it and I additionally have you bookmarked to see new things on your site.

    ReplyDelete
  8. We provide Spanish language course Patiala that are implemented by trained and experienced professionals. We provide video-based flashcards to review the material, no textbooks are required. We held conversation-based Spanish programs. We provide both options of Learn Spanish language Patiala or live classes with us. We too give an opportunity of a free trial with our well-experienced professionals.

    ReplyDelete
  9. want simplest take a look at the ideal Scorecard to understand which standards the code in front of them needs to meet to align with the rest of the software. Better Breathing Sport The Scorecards can be made the usage. Chillwell AC of data from custom resources and Query Language (CQL). CQL additionally allows the querying of records in. order that authors and reviewers alike can fast locate solutions for your team’s documentation to any questions they have which might be applicable to the review handy. https://www.outlookindia.com/outlook-spotlight/better-breathing-sport-device-reviews-2023-does-it-work-latest-warning-info-detailed-price-news-277628
    https://www.outlookindia.com/outlook-spotlight/chillwell-portable-ac-2023-does-it-work-or-latest-scam-uses-benefits-risk-price-news-279631

    ReplyDelete

  10. Language Studio is a renowned institute that offers comprehensive Spanish language courses Patiala. Their courses are designed to cater to learners of all levels, from beginners to advanced speakers. With experienced teachers and a well-structured curriculum, Language Studio provides an immersive learning experience that helps students develop their Spanish language skills quickly and effectively. Whether you want to learn Spanish for personal or professional reasons, Language Studio has a course that suits your needs.

    ReplyDelete
  11. In the bustling city of Chandigarh, where diverse cultures and languages converge, there is a growing fascination with the Spanish language. Whether you are an avid traveler, a language enthusiast, or someone looking to enhance their career prospects, learning Spanish can open doors to a myriad of opportunities. The Languages Studio is a reputable Spanish language institute in Chandigarh, has emerged as a prominent hub for Spanish language courses. In this article, we will delve into the advantages of learning Spanish in Chandigarh, the unique offerings of The Languages Studio, and why it is the perfect destination for aspiring Spanish speakers.

    ReplyDelete
  12. With numerous of benefits such as appetite reduction and improvement of mental health, the best weight loss supplement brings a noticeable difference by blocking for the fat storage. It is an inspirational supplement that does Slimming Gummies Erfahrungennot create any health issues or risk at all. The remedy does not require any prescription or a special effort at all. It helps to shape up your whole body in a glorified way.No user is going to experience the side-effect of using Slimming Gummies. Even in the most temporary stomach pain and discomfort or not felt on choosing the supplement. It has chromium picolinate garcinia Cambodia and other natural extracts that approved and are clinically tested.Dietary supplement is absolutely safe and is mare with hundred percent natural extracts. You should consume the recommended dose of 2 gummies per day and observe changes in your body within a span of 90 days. The effectiveness of weight loss remains natural but maybe subjective in nature.Dietary supplement is absolutely safe and is mare with hundred percent natural extracts. https://www.5min.at/202304642723/slimming-gummies-erfahrungen-test-neueste-bewertungen-2023/

    ReplyDelete
  13. Emblem Technologies is a leading technology consulting firm in the USA, providing innovative solutions to businesses. With a strong focus on digital transformation and cutting-edge technologies, Emblem Technologies offers expertise in areas such as cloud computing, artificial intelligence, cybersecurity, data analytics, and software development. Their team of skilled consultants assists clients in leveraging technology to enhance efficiency, streamline operations, and drive growth. Emblem Technologies is known for its strategic approach, deep industry knowledge, and commitment to delivering tailored solutions that meet the unique needs of each client

    ReplyDelete
  14. Discover the enchanting world of Layesha Lashes. Unveil captivating eyelash extensions and accessories to enhance your natural beauty.

    ReplyDelete
  15. Germany plays a pivotal role in global transportation as a strategic hub in Europe. With world-class airports, well-developed ports, and a sophisticated rail network, it offers seamless connectivity for goods and passengers. Its logistical expertise, combined with efficient customs procedures, makes it an ideal gateway for international trade and travel

    ReplyDelete
  16. Dermatology is the medical specialty that deals with the diagnosis and treatment of skin, scalp, and finger problems. It's a medical and surgical specialty that combines the two.

    ReplyDelete
  17. At DNA Driveways, we are experts at installing a range of different driveways throughout Warrington. Your driveway helps to protect your property from the weight of any vehicles, foot traffic and from the weather. It is also important in helping your home to look attractive!

    ReplyDelete
  18. Red and Black SPorts Fan Baby CLothes. Great for team color onesies. Lil and future athletes need this for thei favorite Football Team won basketball, softball, baseball, swim, track and field and other sports fans are called. Here is your chance to get your baby unique 706 Athens apparel and Represent. uga baby onesie

    ReplyDelete

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.