Salesforce Developer Tutorial - The Complete Guide to Apex Tests in 2024

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ค. 2024
  • In this episode we go over everything you need to know about writing Apex Tests so that you can write high quality tests for the code in your Salesforce org!
    Please consider donating to help continue to improve the content produced for Coding With The Force: www.paypal.com/donate?busines...
    Get Coding With The Force Merch Here: my-store-d20559.creator-sprin...
    Follow all of our social media here:
    Patreon: / codingwiththeforce
    Github: github.com/Coding-With-The-Force
    Facebook: / codingwiththeforce
    Twitter: / coding_force
    Instagram: / codingwiththeforce
    Salesforce development books I recommend:
    Advanced Apex Programming: amzn.to/3lXi8to
    Salesforce Lightning Platform Enterprise Architecture: amzn.to/2R0D4BQ
    Mastering Salesforce DevOps: amzn.to/3lTtTRN
    Good Non-SF Specific Development Books:
    Clean Code: amzn.to/35PuMDU
    Clean Architecture: amzn.to/35FYo6A
    -----------------------------------------------------------------------------------------------------------------------
    Table of Contents:
    0:00 - Intro
    1:10 - What are apex tests and why make them?
    5:10 - Creating our first Apex Test
    14:47 - Running our Apex Test
    16:00 - How to use Assertions
    23:06 - Using the @TestSetup method
    35:22 - Creating a Test Data Factory
    45:51 - Alternative ways to generate test data
    47:18 - The infamous SeeAllData parameter
    50:00 - Testing Private Methods
    53:21 - When to use Test.isRunningTest
    56:27 - Test class best practices
    1:02:32 - Testing Integrations
    1:20:46 - A brief overview of the Test class methods
    1:23:50 - Running your tests as a specific user
    1:28:19 - Difference between a Unit and Integration test
    1:35:15 - Unit Test Overview
    DISCLAIMER: The views and ideas expressed on this TH-cam channel and blog are based on past development experiences and may not always work for your particular scenarios. The advice in these videos may also become outdated as technology advances. They should be used as examples for educational purposes only. Using any shown solutions in your own org is not advisable since they are example scenarios.
    #salesforce #salesforcedeveloper #salesforceadmin

ความคิดเห็น • 24

  • @CodingWithTheForce
    @CodingWithTheForce  6 หลายเดือนก่อน +12

    I've realized (despite spending forever making this lol) in this guide I do not specifically cover testing a trigger in Apex. This is because I have become so used to using trigger handler/domain classes over the years that I end up testing the logic in them the exact same way I test a typical apex class! That said, to test an apex trigger, you simply need to do a dml statement that would trigger the trigger. If you have a Case trigger that triggers after update, you need to update a case in your apex test to fire your trigger. If you have a Contact trigger that fires before or after insert you need to insert a new contact in your test class to fire the trigger, etc. This will cause your trigger to fire and will allow you to gain coverage of your trigger. However, if the bulk of your trigger logic is in your triggers, I would strongly suggest moving them to a trigger handler or domain class! It will make your life so much better and easier as a dev! I even have a video covering it :)
    Additionally in this video when I describe what happens in a @TestSetup I state that the data is deleted... this was a bad attempt at simplifying the process that takes place. What actually happens is the data is rolled back to its original testsetup state at the end of each test method execution. In more complex testsetup situations I have seen, in the pas,t that testsetup rollbacks end up adding to governor limits, which is why I've mentioned it here.

    • @diobrando1253
      @diobrando1253 6 หลายเดือนก่อน +1

      I’m glad I found out your channel ❤ thanks for the videos hope you keep posting and us keep learning from you

    • @luisescalante4897
      @luisescalante4897 4 หลายเดือนก่อน

      Thank you so much for making this video Matt, I have found it very helpful! Just a quick question, so, if we are using DataFactory, we will use it in the @TestSetup? Or DataFactory methods can be used directly inside of the test methods?

    • @CocoricoFlux
      @CocoricoFlux 3 หลายเดือนก่อน

      Hello Matt, thank you for all your video, I'm getting to understand Apex !
      I'm halfway through the video but the part about testsetup was surprising for me. I worked for an ISV where our devs struggled to make some test because we had a ton of data to create beforehand and I was thinking: "maybe they should have split their test in multiple classes with the same test setup".
      You already corrected it in your comment but I would like to emphasize that the test setup run only once and only rollback between test if necessary.
      Here is the paragraph from the Salesforce documentation "If a test class contains a test setup method, the testing framework executes the test setup method first, before any test method in the class. Records that are created in a test setup method are available to all test methods in the test class and are rolled back at the end of test class execution. If a test method changes those records, such as record field updates or record deletions, those changes are rolled back after each test method finishes execution. The next executing test method gets access to the original unmodified state of those records."

  • @iamTHIEN013
    @iamTHIEN013 หลายเดือนก่อน

    This guy is a God among Safeforce dev, I'm so grateful. Thank you so much

  • @cmgpguerracarlos
    @cmgpguerracarlos 6 หลายเดือนก่อน

    Thanks for all your videos; they really helped me with fflib, and now I understand testing better.

  • @tornubarikoboo8074
    @tornubarikoboo8074 6 หลายเดือนก่อน

    Great Video!

  • @NachopUY
    @NachopUY 2 หลายเดือนก่อน

    This is a real masterclass. Thank you thank you thank you!!

  • @CrisjunMarRavelo
    @CrisjunMarRavelo 6 หลายเดือนก่อน

    Nice, new Video ❤

  • @pedroprada1
    @pedroprada1 2 หลายเดือนก่อน

    great trainign!

  • @assiabensaada1852
    @assiabensaada1852 5 หลายเดือนก่อน

    i press like button even before watching your video! i'am starting my journey soon as a salesforce developper in Paris thank you !

  • @shashi.shekhar
    @shashi.shekhar 6 หลายเดือนก่อน

    We can also consider bulkifying the test methods with bulk test data to check if the code can handle large amount of data (real life production scenario) without failing for exceptions (101/501) etc

  • @shaunvanhuyssteen7966
    @shaunvanhuyssteen7966 5 หลายเดือนก่อน

    Heya Matt, this is fantastic thanks man!
    I still need to go through the whole video but getting testing right is something that we've been struggling with in our team so getting a good idea on best practices will help a lot!
    I was also wondering, do you have any recommendations on resources where a developer can learn more about AI in Salesforce? Everything I've seen, including the AI associate, seems high level. I would love to learn how to implement custom NLP models and LLM in Salesforce

  • @levon9
    @levon9 6 หลายเดือนก่อน

    Very helpful. I just ran the first test, and I noticed that my output table (?) looks different from yours at time 16:00. I don't have the "CLASSES" "PERCENT" "UNCOVERED LINES". Instead I get a summary that has two columns, NAME and VALUE which shows some information (incl Pass Rate 100%). Just curious why my output would be different. Win 10 with latest version of VSCode.
    Thank you so much for making this video. I've already picked up some useful information & reminders in the 16 minutes I've watched, looking forward to the rest.

  • @DaDudeMan
    @DaDudeMan 4 หลายเดือนก่อน +2

    LWC masterclass when?

    • @CodingWithTheForce
      @CodingWithTheForce  2 หลายเดือนก่อน

      I hope to release the entire course in ~2 months

  • @sheddy3561
    @sheddy3561 4 หลายเดือนก่อน

    My man switched to VS Code😀

  • @ranjithshastry9961
    @ranjithshastry9961 2 หลายเดือนก่อน

    Hi @CodingwithTheForce can i Please know the lwc master series which your planning which i saw in one of the vedio could you please let us know when your Planning to upload Eagerly waiting,Thank you!

    • @CodingWithTheForce
      @CodingWithTheForce  2 หลายเดือนก่อน

      I hope to release the entire course in ~2 months

  • @extremeweirdness1528
    @extremeweirdness1528 6 หลายเดือนก่อน

    Hi is there a way to do multiple callouts and set different mocks for each one ?

    • @wslee00
      @wslee00 6 หลายเดือนก่อน

      have your mock hold a list of responses and pop each one out as callouts are made

  • @richhuynh6314
    @richhuynh6314 6 หลายเดือนก่อน

    First I’m first

  • @CloudCodeInsights
    @CloudCodeInsights 6 หลายเดือนก่อน

    Is there anything in documentation that outlines that test setup runs per each test method? I tested in a dev edition and I'm only seeing the DML's run once per the entire test run which includes multiple test methods.

    • @cassioorsi
      @cassioorsi 5 หลายเดือนก่อน

      TestSetup only runs once.
      Then the test methods run.