function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ASHISH RAI 103ASHISH RAI 103 

Can anyone explain me what below three test methods are doing?

@isTest
public class TestVerifyDate {
    @isTest static void testOldDate(){
        Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(-1));
        System.assertEquals(date.newInstance(2016, 4, 30), dateTest);
    }
    
    @isTest static void testLessThan30Days(){
        Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(20));
        System.assertEquals(date.today().addDays(20), dateTest);
    }
    
    @isTest static void testMoreThan30Days(){
        Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(31));
        System.assertEquals(date.newInstance(2016, 4, 30), dateTest);
    }

}

 
Best Answer chosen by ASHISH RAI 103
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Ashish,
These methods are unit tests for verifyDate class.Unit tests methods are class methods that verify whether a particular piece of code is working properly or not.
These methods are testing checkDates method with different inputs.
Assert statements are used to compare the output with the expected value.
System.assertEquals() method, which takes two parameters: the first is the expected value, and the second is the actual value.If the expected value and the actual value are same then assertion passes else it assertion fails.
@isTest
public class TestVerifyDate {
    @isTest static void testOldDate(){
        //invokes checkDates methods by passing 2 date parameters with different values
        Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(-1));

        //verifying the result given by the checkDates method with expected output
        //In this case 2016-04-30 00:00:00 is the expected output
        System.assertEquals(date.newInstance(2016, 4, 30), dateTest);
    }
    
    @isTest static void testLessThan30Days(){
        Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(20));
        System.assertEquals(date.today().addDays(20), dateTest);
    }
    
    @isTest static void testMoreThan30Days(){
        Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(31));
        System.assertEquals(date.newInstance(2016, 4, 30), dateTest);
    }

}
Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Ashish,
These methods are unit tests for verifyDate class.Unit tests methods are class methods that verify whether a particular piece of code is working properly or not.
These methods are testing checkDates method with different inputs.
Assert statements are used to compare the output with the expected value.
System.assertEquals() method, which takes two parameters: the first is the expected value, and the second is the actual value.If the expected value and the actual value are same then assertion passes else it assertion fails.
@isTest
public class TestVerifyDate {
    @isTest static void testOldDate(){
        //invokes checkDates methods by passing 2 date parameters with different values
        Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(-1));

        //verifying the result given by the checkDates method with expected output
        //In this case 2016-04-30 00:00:00 is the expected output
        System.assertEquals(date.newInstance(2016, 4, 30), dateTest);
    }
    
    @isTest static void testLessThan30Days(){
        Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(20));
        System.assertEquals(date.today().addDays(20), dateTest);
    }
    
    @isTest static void testMoreThan30Days(){
        Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(31));
        System.assertEquals(date.newInstance(2016, 4, 30), dateTest);
    }

}
Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
ASHISH RAI 103ASHISH RAI 103
Thank you, Devi for the quick reply, however i want to know what the below method will put as its output if you can give an example also it would be very helpful as i am new here :) Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(20)); System.assertEquals(date.today().addDays(20), dateTest);
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Ashish,
1)Date dateTest = VerifyDate.CheckDates(date.today(), date.today().addDays(20));
The output of this method depends on the logic written in  verifyDate class checkDates method.
In your org there will be a class with name verifyDate and it contains a method with name as Checkdates.Here the test class method testOldDate is invoking the checkdates method of verifyDate class to verifythat Checkdates is working properly or not.
2)System.assertEquals(date.today().addDays(20), dateTest);
After  invoking the method we should write assert statements to check if the output of the method we are testing and the expected output matches or not.Assert fails if it doesn't match ,this indicates that your logic is not working as expected.
Test classes are not meant for output.They are written to check if the code is working properly and doesn’t break in any situation in Production.
In salesforce inorder to deploy code to production it must have Minimum 75% code coverage.So we have to write test classes for every apex class and trigger in salesforce.

Please refer below links which might help you further.
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unit_tests.htm

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
ASHISH RAI 103ASHISH RAI 103
thank you understood now.