• +91 9311 5511 17
  • info@websharpmarketing.com
Web Sharp Marketing Web Sharp Marketing
Get A Quote
  • Home
  • About Us
  • Our Services
    • Full Stack Development
    • Salesforce Training
    • Salesforce Solutions
    • Data Migration and Processing
    • Code Review, QA and Support
    • Website Designing
  • Blog
  • Contact Us
  • February 24, 2024
  • WSM
  • 0 Comments
  • 234 Views
  • 2 Likes
  • Apex

What is Test class in Salesforce Apex

We know that Testing is an important part of SDLC(Software Development Life Cycle).  In Salesforce when you want to deploy classes or Triggers from one org to another org, Salesforce requires at least 75% of your code to be covered by the test classes.

So,  Salesforce wants to make sure that our code doesn’t break in any situation in Production. Today we’ll see how we write the test class with example in Salesforce.

Some points which you have to know regarding the test classes in Salesforce:

  1. At least 75% of your Apex code must be covered by unit tests, and all of those tests must be completed successfully. But this should not be our focus. We should aim for 100% code coverage, which ensures that you cover each positive and negative use case of your code to cover and test every branch of your code.
  2. Calls to System.debug are not counted as part of Apex code coverage.
  3. Test methods and test classes are not counted as part of Apex code limit. So, no worries about writing long test class with more methods just to make sure that all your code branches are covered.
  4. Every trigger you are trying to deploy should have at least 1% coverage, but yes overall coverage of your production org after getting your code deployed should be 75%, otherwise Salesforce won’t let you deploy your code.
  5. Class can be deployed on 0% coverage as well, but as I told in last point, that overall coverage of your production org after getting your code deployed should be 75%, otherwise Salesforce won’t let you deploy your code.

In the example below, we will learn how to write a very simple test class:

1
2
3
4
trigger HelloWorldTrigger on Book__c (before insert) {
  Book__c[] books = Trigger.new;
  MyHelloWorld.applyDiscount(books);
}

Trigger Helper Class:

Test Class Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@isTest
private class HelloWorldTestClass {
 static testMethod void validateHelloWorld() {
 Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100);
 System.debug('Price before inserting new book: ' + b.Price__c);
 
 Test.startTest();
 // Insert book
 insert b;
 
 Test.stopTest();
 
 // Retrieve the new book
 b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id];
 System.debug('Price after trigger fired: ' + b.Price__c);
 
 // Test that the trigger correctly updated the price
 System.assertEquals(90, b.Price__c);
 }
}

The key points while writing a test class are:

  1. You have to start your class with @isTest annotation, then only Salesforce will consider this class as a test class.
  2. Keep your class Private, and the best practice is to name your test class as your original Class or trigger Name + ‘Test’.
  3. Methods of your test class have to be static, void, and the testMethod keyword has to be used.
  4. Prepare your test data which needs to exist before your actual test runs. There are multiple techniques for creating test data nowadays, for example, setup method, static resources, etc. For more details about @testsetup method check below link @testSetup method in apex test class
  5. Use Test.startTest() and Test.stopTest() to make sure that the actual testing of your code happens with the fresh set of governer limits. These methods help you to reset your governor limits just before your actual code of testing gets executed.
  6. Once your test code runs between Test.startTest() and Test.stopTest(), you must use assert statements to test whether your actual code is executing correctly and giving the results as expected. In our case, we are testing whether book’s price has been set to 90 or not. If this assert statement returns false, then your test class will fail, and will let you know, that something is not correct in your code, and you need to fix your original code.
  7. Because we are testing a simple trigger, we could not show the testing using negative use cases, but in an ideal world, you should write multiple methods in your test class, few should test your positive use cases, and other should test your negative test cases.

Resources: 

Trailhead test class Module

Salesforce Developer document for test class

Thanks for reading this article, we will provide you more articles related to Apex salesforce. Good Luck 🙂

Tags:
@isTestApexsalesforce DeveloperTest Classtest Class Code Coverageunit Test
Prev PostSalesforce Data Migration with DataLoader or Data Import Wizard
Next PostTestSetUp method in apex test classes
Related Posts
  • Lightning Component(Aura) To Display Contacts Related To Particular Account August 22, 2019

Leave a Comment Cancel Comment

Your email address will not be published. Required fields are marked *

WSM Technosoft’s offering enables clients to quickly augment their Salesforce Consultant teams through product development and support resources located in our development centers. Based upon project’s requirements, WSM Technosoft’s Team can either co-locate at client at site or work from our development center in India.

Our Services

  • Website Designing
  • Salesforce Solutions
  • Salesforce Training
  • Data Migration and Processing
  • Code Review, QA and Support

Recent Articles

  • How to get your work history report

    Aug 21, 2024

  • How Salesforce Delivers Exceptional

    Mar 20, 2024

Useful Links

  • Home
  • Portfolio
  • Contacts
  • Services
  • FAQ
  • About Us
  • Services
  • Blog
  • About Us
  • Contacts
info@websharpmarketing.com
+91 9311 5511 17 Call Us Now
2nd Floor, Devasthali - Plot no. 42/5, Block – A, Sector – 62, Noida, UP - 281203
© WSM Technosoft, 2014–2024. All rights reserved