 Hello, everyone. Welcome to Sanjay Gupta Tech School. I'm Sanjay Gupta and this is my website, studysalesforce.com, a video library to learn Salesforce. In this video, I'm going to demonstrate how you can implement test classes for your Apex code. So, first I'm going to implement a test class, sorry, first I'm going to implement a Apex class. So, I'm creating new Apex class, I'm naming it as temperature. So, in this class, I'm going to implement a method and then I will show you how we can write test class for that. So, in my previous video, I discussed about Apex test classes theoretically, but in this video you will get to know practical implementation. So, I will be implementing three to four videos where I will be demonstrating different type of test class implementation. So, this is the first one. So, I'm implementing this class method now. So, public, static, decimal, finite to centigrade. So, here I'm going to receive value in decimal. So, here I'm creating one variable as result. Decimal result equals to, so now I'm going to apply formula FH minus 32 multiplied by 5 upon 9. And result, return, result.setScale. So, here I'm setting scale as 2. So, this way I implemented this Apex class whose name is temperature. Now, after implementing this Apex class, I need to implement its test class. So, I'm going to create one more Apex class. Now, see here how to implement Apex test class name. So, your Apex class is temperature, so you can write temperature test. So, always remember this thing whenever you try to implement test classes. So, name of test class will match with your actual Apex class. So, whenever a developer will be searching temperature class, so its test class will be searched automatically. Now, you need to put adverite is test annotation before starting the class. And now I'm going to write a method through which I will be covering this method. So, for that also I need to write adverite is test. Now, private static void warm temperature test. So, this is basically a method which will be calling the method which is defined here on head to centigrade. So, here I'm creating a decimal variable res equals to. So, I need to call the method of temperature class. So, this way I'm going to call it and here I'm passing 70. So, 70 is for net value and this method for net to centigrade will be converting this 70 into centigrade and that result will be available into areas. Right, so we are calling temperature class for net to centigrade method, which is defined here and it is a static method so we can call it directly. Now, after getting the value in areas variable, we can apply system dot assert equal. Right, so here you need to put two values. First will be expected and second will be actual. So, expected should be 21.11. So, if 70 is for net so it's equaling centigrade is 21.11. Now, here actual value will be available into areas. So, this way we need to match both the values if both are matching. It means this system dot assert equal will be passed. So, in previous video I told you two things. First is we need to have code coverage more than 75% and second requirement is all your tests should pass. So, this assert equal will ensure like your test is passing or failing. Right, so now I'm saving this code. So, this is the way you can implement your test class. So, basically this is a simplest example. So, we are having a simple code which is converting for net into centigrade. Now, here we are calling that method and we are checking the resultant value if resultant value matches with the expected value. It means your code is working fine. Otherwise, there is some issue. So, I saved this test class. Now, you need to understand how we can run this test class. So, here you can see this is a pick class and at right-hand side you can see only go to button is available. But if I go to temperature test class, so here along with go to run test button is also available. And at bottom you can see this test tab is available. So, here you will see whatever test you will be running. So, you need to open your test class and you need to click on this run test. So, it is running your test synchronously. Here you can see the results. You can click on this plus sign to expand the results. So, we implemented only one method in the test class. So, that is passed because green tick is available here. If it got failed, so failure will be zero and it should have displayed as a cross sign. That isn't radical. So, I will show you that as well. So, I just ran this test class. So, it is executed completely. It is passing because of this green right sign. You can identify that. Now, how you can see your actual apex is covered. Like you need to test the code coverage. So, here you need to open the temperature class and you can click on this button and here you can select this all test. So, this way you can see like how many lines are covered and how much percentage code is covered. So, here 100% code is covered and all lines are covered. So, this is the basic example like how you can implement your apex test class. Now, I will show you how test fails. So, if I write here 20 instead of 20.11, right? So, result will be 21.11, but I am expecting 20.11. So, both won't be matching. So, in this case, test will fail. So, let's test this as well. So, I am clicking on run test. So, test is executed and here you can see this cross sign is available. It means test failed. Failures are one and total was one. So, if I expand this, so it will show the error message as well. You need to double click on this. So, here you can see just double click on this error. So, you will see assertion failed expected was 20.11, but actual was 21.11. So, that's why your test failed. So, in real-time scenarios, why you will be implementing your test methods? So, it happens. Like you expecting some value and result is something else. So, both won't be matching. It means your tests are not passing. If your tests are not passing or assert equal method is failing, it means something is wrong in your apex code. So, right now, knowingly, I showed you like how test fails, so that you can recognize. But in our case, actually it is passing. And if I go here, so here you can see 100% code coverage is there. Still, your tests are failing. So, why 100% code is covered? Because method was called, it is returning some value. So, lines are executed through test class, but tests are not passing. So, you need to ensure both the things like 100% or maybe more than 75% code should be covered and your tests should pass. Like system.assert equals method should pass. So, right now code is covered, but this method is not passing. It means our test class is not implemented properly. Now, I'm going to implement one more method. So, I'm just copying this, pasting it here. Now, I'm just checking for another cold temperature test. So, you can write more than one methods like this. So, here I'm going to write zero and I'm saving this code. Running test. So, you can see again, it is failing. So, I need to test. So, you can see warm temperature test is passing and cold temperature test is failing. So, this can also happen. Like, if you implemented two methods, one is passing, one is failing. So, this also happens. So, total methods are two, one is passing and one is failing. So, which one has failed, you need to double click on that. Open the error message. So, here you can see expected zero actual is 1.11. So, that's why it was failing. Right? So, whatever the result of conversion is, it should match. Right? Now, here I'm passing 32. So, 32 Fahrenheit will convert into zero centigrade. So, now I'm closing all these things. So, here you can see one is passing and two tests failed. So, now I updated this one, this code again, running test. And here you will see both tests are passing. Both are passing completely. No error message is available. And if I see this code, so it is covered 100%. So, both the requirements are fulfilling. Code is covered 100% and whatever test method I implemented, they are passing as well. So, this is the simplest example through which you can understand like how we can implement test classes. So, I gave you a walkthrough like how to implement test classes, how to run tests, how to check like whether test passed or failed, how to check the code coverage. So, I hope through this demonstration, you understood implementation of test classes, but don't forget to watch another videos as well. In those videos, I will be implementing some complicated examples. So, that will help you. So, those are available on my YouTube channel and those are also available on my study salesforce.com website. So, do visit that and there you will find all the videos arranged in a proper order. So, that website is this. So, you can visit this and you can get expertise in Salesforce ecosystem. Thank you for watching this video.