 Hello, everyone. Welcome to Sanjay Gupta Tech School. This is my website, studysalesforce.com, a video library to learn Salesforce. In this video, I'm going to discuss about Apex Test Class. So introduction to Apex Test Class. The Apex testing framework enables you to write and execute tests for your Apex classes and triggers on the Lightning platform. So in Salesforce, whenever you write any Apex class or if you implement any trigger, so for trigger, you need to implement Apex code as well. So after implementing those Apex codes, you need to implement test classes as well so that you can identify the Apex classes those you implemented are properly working or not. So Apex unit tests ensure high quality for your Apex code and let you meet requirements for deploying Apex. So we can deploy our Apex code from one org to another. So let's say if you are working in Sandbox, then you will deploy your code into production. So in that case, you need to implement Apex test classes so that your Apex code can be deployed properly. So Apex code can only be written in a Sandbox environment or a developer org not in production. So every time whenever you want to implement Apex code, so you need to write that in Sandbox environment or in the developer org, you cannot implement Apex code directly in the production. So while implementing Apex code in Sandbox environment or in developer org, you need to implement its test class as well. If that test class is implemented, so you can now deploy both Apex code along with its test class from Sandbox to production. Further, Apex code can be deployed to a production org from a Sandbox. Also, app developers can distribute Apex code to customers from their developer orgs by uploading packages to the Lightning platform app exchange. In addition to being critical for quality assurance, Apex unit tests are also requirement for deploying and distributing Apex. So if you are implementing Apex code and if you are deploying or distributing those Apex code, so to implement their test classes is the requirement. Otherwise, you cannot deploy or distribute your Apex classes. So now we need to discuss about benefits of Apex unit tests. So ensuring that your Apex classes and triggers work as expected. So this is the primary benefit of implementing Apex unit tests with the help of Apex test classes. Having a suite of regression tests that can be rerun every time classes and triggers are updated to ensure that future updates you make to your app don't break existing functionality. Meeting the code coverage requirements for deploying Apex to production or distributing Apex to customers via packages. So to achieve all these requirements, we need to implement Apex test classes whenever we implement Apex classes. Next, in the series, high quality apps delivered to the production org, which makes production users more productive. So if you implement test classes, it means you are testing your actual Apex code through those test classes. So if you do that, it means you are delivering high quality apps. So it basically increased the productivity of your users. So high quality apps delivered to package subscribers, which increase your customers trust. So if your code is error free, it is up to the mark. So it also increase your trust in the customer. So that's why whenever you write Apex code, so it is a best practice to write Apex test class as well. Now we need to discuss the code coverage requirement for deployment. So basically when you implement Apex test classes, so it covers actual Apex class. So that term is known as code coverage. So we through Apex test class, we need to check the code coverage of the Apex class for which you are implementing Apex test class. So in this video, I am explaining everything theoretically. But in the next video, I will be implementing Apex class, then it's Apex test class. And then I will show you how we can see the code coverage through those test classes. So before you can deploy your code or package it for lightning platform app exchange, at least 75% of Apex code must be covered by test and all those tests must pass. So this is the bare minimum requirement. If it doesn't happen, it means you cannot deploy your code to production. In addition, each trigger must have some coverage. Even though code coverage is a requirement for deployment, don't write test only to meet this requirement. So for deployment, minimum 75% code coverage is required. But don't focus on this. You need to focus on this as well like if you are writing test classes. So you need to write all positive and negative tests so that you can see the actual Apex code that you implemented is working properly or not. So make sure to test the common use cases in your app, including positive and negative test cases and bulk and single record processing as well. So you can implement more than one methods to test a particular Apex method. Like if you have implemented Apex class and it is containing one method. So in Apex test class, you can implement two or three methods so that you can verify that one method that is implemented in the Apex code. So this is basically known as positive or negative testing. And also in test class, you can create bulk data so that you can test your actual Apex code with the help of that bulk data. So if that is working properly, it means the code you have implemented in Apex that is up to the mark. So now we need to discuss some important things that we need to remember. So while implementing test classes, false to system.debug are not counted as part of Apex code coverage. So in your Apex class for which you are going to implement Apex test class. So if system.debug are implemented so they are not counted as a part of code coverage. They are ignored simply. And as a best practices, we generally don't write system.debug in Apex class. We use them for testing purpose for debugging purpose. But when your code is ready, so you need to remove all those system.debug from your actual Apex code. Test methods and test classes are not counted as part of Apex code limit. So no worries about writing long test class and more methods just to make sure that all your code branches are covered. So this is also important point that you need to remember. And class can be deployed on 0% coverage as well. But that overall coverage of your prediction or after getting your code deployed should be 75%. Otherwise Salesforce won't let you deploy your code. So these things you need to remember while working with Apex and Apex test classes. Now after too much theoretical discussion, this is the time to understand how we can write test methods. So basically you need to use this adverite is test annotation. Then you can write static void then test method name and then the code block. Or you can write static then test method void test name. So this test method is a keyword so you can use that as well. So generally we prefer this first annotation that is adverite is test and then we define the method. Now how we implement the complete class along with the methods. So this is the syntax. So whenever you are writing Apex test class. So you need to write adverite is test then private class might as class. Then again adverite is test for each of the method that will be working as a test method. So static it should be static because it will be called automatically then return type and test method name and then the code block. And you can implement as many test method as required so that you can increase the code coverage of your Apex class. So this is all about introduction to Apex test class. So don't forget to watch another video that is available just after this. So in that video I will be demonstrating how to implement Apex test classes with the help of this. Thank you for watching this video. Don't forget to visit study salesforce.com where you can find all the videos.