Salesforce Apex: Exception Testing, Test.startTest & System.runAs - Part 4

Опубликовано: 18 Июнь 2026
на канале: Code With Sally
390
13

🚀 Final Session on Apex Unit Testing! Ready to master positive & negative testing, exception testing, Test.startTest & Test.stopTest, and executing tests as different users with System.runAs? In this final part of our Apex Unit Testing Series, we focus on techniques to make your Salesforce unit tests more effective and scalable.

🌟 What You’ll Learn:
✔ Positive & Negative Testing – Learn how to test both expected behaviors and failure scenarios.
✔ Exception Testing – Writing negative test scenarios and handling expected failures.
✔ Using Test.startTest & Test.stopTest – Managing governor limits in test classes.
✔ Creating & Running Tests as Different Users – Using System.runAs() in unit tests.
✔ Live Code Examples – Hands-on test class implementations for OrderUtility & AccountService.
✔ Unit Testing Best Practices & Next Steps – Key takeaways and upcoming advanced topics.

📂 *Code Samples from This Session*
👉 [Session 35 - Apex Test Classes on GitHub](https://github.com/CodeWithSally/Code...)

📌 Timestamps:

00:00 – Welcome to Code with Sally & Session Introduction 🎉
00:58 – Recap: What We Covered in Apex Unit Testing So Far
02:23 – Today's Agenda: Positive & Negative Testing, Exception Testing, Test.startTest & System.runAs
04:18 – Positive vs. Negative Testing in Apex Unit Tests (valid input vs. invalid input & expected failures)
07:43 – Reviewing the OrderUtility Class & Identifying Test Scenarios
14:36 – Live Coding: Writing a Positive Test Method for OrderUtilityTest Class
17:45 – Live Coding: Writing a Negative Test Method for Exception Testing (try-catch & Assert.fail)
25:47 – Homework & Extra Practice for Negative Testing
27:48 – Introduction to Test.startTest & Test.stopTest in Apex Unit Tests
28:11 – Live Coding: Implementing Test.startTest & Test.stopTest in Test Classes
39:10 – Slide Explanation of Test.startTest & Test.stopTest
41:11 – Introduction to System.runAs & Creating Users in Test Classes
41:44 – Live Coding: Can We Create Users in Unit Tests? Will They Be Rolled Back?
48:41 – Live Coding: How to Use System.runAs to Execute Tests as Different Users
59:49 – Live Coding: Combining Test.startTest with System.runAs for Testing
1:01:00 – Slide Explanation of System.runAs & Why It’s Important
1:02:00 – Key Takeaways: Summary of Apex Unit Testing Concepts
1:04:00 – Sneak Peek: Advanced Unit Testing Topics for Future Sessions
1:05:00 – Homework, Encouragement to Practice, and Session Closing

💬 Have Questions?
Drop your thoughts or coding challenges in the comments below!

🔔 Subscribe and hit the bell icon to stay updated with new videos.

🔗 Helpful Links:
Previous Video: Salesforce Apex: testSetup, Test Data Factory & Bulk Testing - Part 3 [   • Salesforce Apex: testSetup, Test Data Fact...  ]
Code With Sally Blog: [https://codewithsally.com/]
LinkedIn: [  / sallyelghoul  ]

#SalesforceApex #ApexUnitTests #CodeWithSally #TestClasses #PositiveTesting #NegativeTesting #ExceptionTesting #TestStartTest #TestStopTest #SystemRunAs #Trailblazers #SalesforceDevelopment #ApexTesting

=======================================
Music by: Bensound
License code: GNYZDOEZVVF2TPB5

Music I use: Bensound
License code: M7MHUXOM6FXFT3MQ