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
monika shewalemonika shewale 

Getting Started with Apex Unit Tests

hi
i am new to salesforce platform.
Install a simple Apex class, write unit tests that achieve 100% code coverage for the class, and run your Apex tests.The Apex class to test is called 'VerifyDate', and the code is available here. Copy and paste this class into your Developer Edition via the Developer Console.
'VerifyDate' is a class which tests if a date is within a proper range, and if not will return a date that occurs at the end of the month within the range.
The unit tests must be in a separate test class called 'TestVerifyDate'.
The unit tests must cover scenarios for all lines of code included in the Apex class, resulting in 100% code coverage.
Run your test class at least once (via the Developer Console) before attempting to verify this challenge.

In the above challege i am not able to write a 'TestVerifyDate' class. I dont unsertstand how to write it for a date.
plz help..
Sagar PareekSagar Pareek
VerifyDate is a class in your salesforce.com org. Your requirement is to write a test class. So the first step is to create a new class name it as TestVerifyDate . Now write @isTest in the very first line of your class and proceed writting the code which calls every and every method of your made class , the basic motive should be to satisfy each and every condition in your VerifyDate class. 

I would recommend you to go thru this trail head module and complete the course , after this you will ready to write a test class 

https://developer.salesforce.com/trailhead/apex_testing/apex_testing_intro
Neil Brown 13Neil Brown 13
@isTest
private class TestVerifyDate {
    static testMethod void TestVerifyDate() {
      VerifyDate.CheckDates(System.today(),System.today().addDays(10));
       VerifyDate.CheckDates(System.today(),System.today().addDays(78));
    }
}
monika shewalemonika shewale
thanks neil... :)
LeninLenin
Hello,

I get 0% test coveragefrom both the above Code from "Neil Brown 13" and my code.
plz help. What did I wrong?
"
@isTest
private class TestVerifyDate{
    @isTest
    static  void TestVerifyDate1() {
        //check for date2 being in the past
        Date day1 = Date.newInstance(2015, 1, 11);
        Date day2 = Date.newInstance(2015, 1, 10);
       
        Date date_res1 = VerifyDate.CheckDates(day1,day2);
        system.debug(date_res1+' Run1: Date1: ' + day1+ 'Date2: ' + day2);
    }  
   
    @isTest
    static  void TestVerifyDate2() {
        //check that date2 is within (>=) 30 days of date1
        Date day1 = Date.newInstance(2015, 1, 10);
        Date day2 = Date.newInstance(2015, 1, 15);
       
        Date date_res1 = VerifyDate.CheckDates(day1,day2);
        system.debug(date_res1+' Run1: Date1: ' + day1+ 'Date2: ' + day2);    }
   
   @isTest
    static  void TestVerifyDate3() {
        //else
        Date day1 = Date.newInstance(2015, 1, 10);
        Date day2 = Date.newInstance(2015, 2, 17);
       
        Date date_res1 = VerifyDate.CheckDates(day1,day2);
        system.debug(date_res1+' Run1: Date1: ' + day1+ 'Date2: ' + day2);
    }
}
"
LeninLenin
This is a dubug result
09:44:21:094 USER_DEBUG [10]|DEBUG|2015-01-31 00:00:00 Run1: Date1: 2015-01-11 00:00:00Date2: 2015-01-10 00:00:00
09:44:21:115 USER_DEBUG [20]|DEBUG|2015-01-15 00:00:00 Run2: Date1: 2015-01-10 00:00:00Date2: 2015-01-15 00:00:00
09:44:21:129 USER_DEBUG [29]|DEBUG|2015-01-31 00:00:00 Run3: Date1: 2015-01-10 00:00:00Date2: 2015-02-17 00:00:00
LeninLenin
So I resolved the problem by running the test from web page "Setup-ApexTest-Run Test" .
I don't undestand why the test run in DevCons did not produced the coverage
Tim Andrews 72Tim Andrews 72
I found that @Neil Brown 13's code works fine in the Dev Console if you check the "Always Run Asynchronously" checkbox. I tried several other times before doing this and it always showed no code coverage, but as soon as I checked that box, it ran and gave 100% coverage and satisfied the challenge.
Trail head 40Trail head 40
@istest
public class TestVerifyDate {
public static testmethod void verifyDate()
{   
   date d=system.today();
   date d1=date.parse('12/05/2016');
   date d2=system.today()+1;
   VerifyDate.CheckDates(d,d1);
   VerifyDate.CheckDates(d,d2);
}
}
Ryan William SchorrRyan William Schorr
There is a known bug that is affecting nearly everyone on the Winter '16 release. This release includes the new Always Run Asynchronously checkbox at the top of the Test menu item in the Developer Console. With the expected functionality, tests should result in code coverage being reported even when this box is unchecked, but the bug is causing tests that aren't run asynchronously to report as 0%. You can check to see if your server* is affected in the "Is it fixed?" section of this success community post (https://success.salesforce.com/issues_view?id=a1p30000000eMoyAAE).

If you can't pass this challenge, use the following troubleshooting steps:
  1. Check the Always Run Asynchronously box as Tim Andrews posted above and run the test.
  2. If the challenge still doesn't pass, copy the code of your test, delete TestVerifyDate.apxc, and create it again with the same code. Run the test.
  3. If it still fails, consider using the exact code Neil Brown posted above.
Don't forget to run the test at least once! The challenge won't pass without VerifyDate being covered at 100% in the Overall Code Coverage box found under the Tests tab in the bottom pane of the Developer Console.

* To find your server, use the lookup tool (http://trust.salesforce.com/trust/domainLookupLaunch/).
vnookala23vnookala23
Hi Ryan William Schorr

I was following the steps to pass the challenge..basically I have created a new test class TestVerifyDate and copied over the code which Nail has posted and did the 'New Run'  and noticed my code coverage is not upto the mark..it was covered only 93% and 

I am not able proceed furtur to having trouble to increase the code coverage 
vnookala23vnookala23
Error I got 

Challenge Not yet complete... here's what's wrong: 
The 'VerifyDate' class did not achieve 100% code coverage via your test methods. Make sure that you run the test in the Developer Console at least once before attempting to verify this challenge
Muhammad AlaaMuhammad Alaa

Try This:

@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);
    }

}

You will just need to change the "date.newInstance(2016, 4, 30)" to the last day of the month when you are running it.
vnookala23vnookala23
Thanks Muhammad Alaa.

It worked now :)
Oleksandr KuzmychOleksandr Kuzmych
This works, too.
@isTest public class TestVerifyDate {
    @isTest static void testVerifyDateTodayPlus29() {
        Date date1 = date.today();
        Date date2 = date1.addDays(29);
        Date dat = VerifyDate.CheckDates(date1, date2);
        System.assertEquals(date2, dat);       
	} 
    
        @isTest static void testVerifyDateTodayPlus31() {
        Date date1 = date.today();
        Date date2 = date1.addDays(31);
		Integer totalDays = Date.daysInMonth(date1.year(), date1.month());
		Date lastDay1 = Date.newInstance(date1.year(), date1.month(), totalDays);    
        Date dat = VerifyDate.CheckDates(date1, date2);
        System.assertEquals(lastDay1, dat);       
	}
}
Or I did something wrong? 0_o
Uttam KavitkarUttam Kavitkar
Thanks Muhammad .

It worked now :)
Paul AlbuquerquePaul Albuquerque
I tweaked Muhammad's code a little so that it works for any date.
 
@isTest
public class TestVerifyDate {
    private static Date dateToday = date.today();
    private static Integer totalDays = Date.daysInMonth(dateToday.year(), dateToday.month());
    
    @isTest static void testOldDate(){
        Date dateTest = VerifyDate.CheckDates(dateToday, dateToday.addDays(-1));
        System.assertEquals(date.newInstance(dateToday.year(), dateToday.month(), totalDays), dateTest);
    }
    
    @isTest static void testLessThan30Days(){
        Date dateTest = VerifyDate.CheckDates(dateToday, dateToday.addDays(20));
        System.assertEquals(dateToday.addDays(20), dateTest);
    }
    
    @isTest static void testMoreThan30Days(){
        Date dateTest = VerifyDate.CheckDates(dateToday, dateToday.addDays(31));
        System.assertEquals(date.newInstance(dateToday.year(), dateToday.month(), totalDays), dateTest);
    }

}


 
SF AnalystSF Analyst

Hi Everyone ,this code is also working good... and easy to understand...

@isTest
public class TestVerifyDate {
    @isTest static void testCheckDates()
    {
        Date D1=system.today();
        Date D2=system.today()+20;
        date D=VerifyDate.CheckDates(D1,D2);
        
    }
     @isTest static void testCheckDates1()
    {
        Date D1=system.today();
        Date D2=system.today()-20;
        date D=VerifyDate.CheckDates(D1,D2);
        
    }
     @isTest static void testCheckDates2()
    {
        Date D1=system.today();
        Date D2=system.today()+60;        
        date D=VerifyDate.CheckDates(D1,D2);
        
    }
  
}

Thanks,
Ghulam
Vladimir Mir 1Vladimir Mir 1
This one will test it to 100% using parse to construct the date (this example uses European date format. Change the date and month around for US)
@istest
private class TestVerifyDate {
@istest static void method1crit1() {
        Date date1 = Date.parse('01/09/16');  //input any date1
        Date date2 = Date.parse('12/09/16');  // date2 
        Date dateexpected = Date.parse('12/09/16');  // expected test value
    
        Date date12 = VerifyDate.CheckDates(date1 , date2);
        System.assertEquals(dateexpected, date12); 
    }
    @istest static void method1crit2() {
        Date date1 = Date.parse('01/09/16');  //input any date1
		Date date2 = Date.parse('12/10/16');  // date2 
        Date dateexpected = Date.parse('30/09/16');  //introduce expected test value
    
        Date date12 = VerifyDate.CheckDates(date1 , date2);
        System.assertEquals(dateexpected, date12); 
    }
	@istest static void submethod1crit3() {
        Date date1 = Date.parse('01/09/16');  //input any date1
		Date date2 = Date.parse('12/11/16');  // date2 
        Date dateexpected = Date.parse('30/09/16');  //introduce expected test value
    
        Date date12 = VerifyDate.SetEndOfMonthDate(date1);
        System.assertEquals(dateexpected, date12);
    }
        
}

 
Ben AllingtonBen Allington

Simple and easy to understand, use both assert statements

 

@isTest 
private class VerifyDateTest {
	@isTest static void CheckDatesTesttrue() {
        Date date1=date.today();
        Date date2=date1.addDays(29);
        Date t = VerifyDate.CheckDates(date1, date2);
        System.assertEquals(t, date2);
    }
    @isTest static void DateOver() {
        Date date1=date.today();
        Date date2=date1.addDays(31);
        Date t = VerifyDate.CheckDates(date1, date2);
        System.assertNotEquals(t, date1);
    }
}
Rajeshwar PatelRajeshwar Patel
Hi all , 
I am getting the following Error :
Method is not visible: VerifyDate.SetEndOfMonthDate(Date)
Sachin Ks 5Sachin Ks 5
I passed the challenge with this code. Let me know if it helps :)
@isTest
private class TestVerifyDate {
    @isTest static void testWithin30Days() {
        Date Datetest = VerifyDate.CheckDates(System.today(), System.today()+10);
        System.assertEquals(System.today()+10, Datetest);
    }
    
     @isTest static void testSetEndOfMonth() {
        Date Datetest = VerifyDate.CheckDates(System.today(), System.today()+52);
        System.assertEquals(System.today()+27, Datetest); <!--27days until last day of Current Month-->
     }
    
    }

 
Angel Montiel 23Angel Montiel 23
Hi all,

I think I don't understand tests. It seems to me that all of this tests you've written are only aplicable to the "CheckDates" method, not the rest of the class. Yet, it works. And I don't understand why. Can you help me out please?

Regards
Suraj TripathiSuraj Tripathi
Hi Angel,
  please try this code. I passed the challenge 100% code coverage  with this code .
 
@isTest
public class TestVerifyDate {
    @isTest public static void test(){
    	 Date Date1 = Date.newInstance(2008, 2, 1);
   		 Date Date2 = Date.newInstance(2008, 1, 1);     
        test.startTest();
        	  VerifyDate.CheckDates(Date1,Date2);
              VerifyDate.CheckDates(Date2,Date1);
       		  VerifyDate.CheckDates(Date2,Date2);
        test.stopTest();
    }
}

 
Swetaleena SalesforceSwetaleena Salesforce
why nobody used codes for DateWithin30Days and SetEndOfMonthDate methods?? bcoz those set as private? pls let me know??
Quinten Poindexter 9Quinten Poindexter 9
You don't see the other methods called because they are executed within the CheckDates method. On it's own, CheckDates doesn't do much other than run an if statment and return two variables. It relies on the the methods further down to assign values to those variables.

I would also suggest using the System class to handle exceptions and ensure the expected outcome matches the actual outcome. I can't think of a real world scenario where you wouldn't want to do this.
 
<pre>@isTest
private class TestVerifyDate {
	@isTest static void testDate2InPast() {
	Date actualDate = VerifyDate.CheckDates(date.newInstance(1990, 1, 2), date.newInstance(1990, 1, 1));
		System.assertEquals(date.newInstance(1990, 1, 31), actualDate);
	}
	@isTest static void testDateWithin30Days() {
		Date actualDate = VerifyDate.CheckDates(date.newInstance(1990, 1, 1), date.newInstance(1990, 1, 2));
		System.assertEquals(date.newInstance(1990,1,2), actualDate);
	}
	@isTest static void testDateGreater30Days() {
		Date actualDate = VerifyDate.CheckDates(date.newInstance(1990, 1, 1), date.newInstance(1990, 2, 2));
		System.assertEquals(date.newInstance(1990, 1, 31), actualDate);
	}
}</pre>

 
Elijah HollingsworthElijah Hollingsworth
I am seeing all these answers with code but that is not helping me understand how I would go about figuring out what to put on my own. I am reading all these "solutions" and I am not understanding how they are getting 100% coverage and how they are testing all the lines of code with such little code in the test class. I have gone over the material a few times and cannot figure out how that material is supposed to give me the information and knowledge to know what to do for this challenge. I want to understand it, not just have a solution :( 
Elijah HollingsworthElijah Hollingsworth
Just to update the above. The comment right above this one did help me understand it better. 
saurabh sengersaurabh senger
Try This, you Will Got 100% Coverage !!

@isTest
public class TestVerifyDate {

    static testMethod void testCheckDate(){
       
       date d1 = VerifyDate.CheckDates(date.today(), date.today().addDays(-1));
        date d3 = VerifyDate.CheckDates(date.today(), date.today().addDays(28));
            }
    static testMethod void testCheckGreater(){
        date d2 = VerifyDate.CheckDates(date.today(), date.today().addDays(30));
    }
}
Ajay K DubediAjay K Dubedi
Hi Monika,
This code work for me. It will help you.
@isTest   
public class TestVerifyDate {      
    @isTest static void testVerifyDate() {     
        Date checkDate1 = VerifyDate.CheckDates(Date.newinstance(2018, 12, 1), Date.newinstance(2018, 12, 11));    
        System.assertequals(checkDate1, Date.newinstance(2018, 12, 11));       
        Date checkDate2 = VerifyDate.CheckDates(Date.newinstance(2018, 12, 11), Date.newinstance(2018, 12, 1));     
        System.assertequals(checkDate2, Date.newinstance(2018, 12, 31));                
    }    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
DAN LI 10DAN LI 10
Hi Guys,
I passed the challenge yesterday and below is the code. It worked fine.
For your reference and hope it's helpful.

@isTest
private class TestVerifyDate {
    
    @isTest static void date2in30days() {
       date result1 = VerifyDate.CheckDates( date.newInstance(2019, 03, 01), date.newInstance(2019, 03, 05) );
        System.assertEquals( date.newInstance(2019, 03, 05), result1 );}
    
    @isTest static void date2beyond30days() {
       date result2 = VerifyDate.CheckDates( date.newInstance(2019,03,01), date.newInstance(2019,04,05) );
        System.assertEquals( date.newInstance(2019,03,31), result2 ) ;}
    
    @isTest static void date2equaldate1() {
       date result3 = VerifyDate.CheckDates( date.newInstance(2019,03,01), date.newInstance(2019,03,01) );
        System.assertEquals( date.newInstance(2019,03,01), result3 );}         
    
    @isTest static void date2beforedate1() {
       date result4 = VerifyDate.CheckDates(date.newInstance(2019,03,01),date.newInstance(2019,02,05));
        System.assertEquals( date.newInstance(2019,03,31), result4) ; }
    }
Ajay K DubediAjay K Dubedi
Hi, Monika shewale,
You can use this test class code to verify the date by using    System.assertEquals. It comes under the best practice of writing code.
@istest
private class TestVerifyDate {
    static testMethod void method1crit1() {
        Date startDate = Date.parse('12/15/15');
        Date endDate = Date.parse('12/16/15');
        Date method1res1 = VerifyDate.CheckDates(startDate, endDate);
        System.assertEquals(endDate, method1res1);
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Hunter AkersHunter Akers
@isTest
public class TestVerifyDate {
	
    @isTest static void testDatesWithin30Days(){
        Date d1 = date.today(); 
        Date d2 = d1.addDays(29);
        Date d = VerifyDate.CheckDates(d1,d2); //Sets variable d equal to CheckDates Results under 30
        System.assertEquals(d2, d); //Verifies that d2 == d
    }
    
    @isTest static void testDateExceeds30Days(){
        Date d1 = date.today();
        Date d2 = d1.addDays(31);
        Date d = VerifyDate.CheckDates(d1,d2); //Sets variable d equal to CheckDates Results over 30
        
        //Using same code to find last day of the month
        Integer totalDays = Date.daysInMonth(d1.year(), d1.month());
		Date lastDay = Date.newInstance(d1.year(), d1.month(), totalDays); 
        
        System.assertEquals(lastDay, d); //Verifies last day of the month == d
    }
}

Test -> Run All 
Check "Overall Code Coverage" should be 100% for this specific Class.
Complete Task

Hunter
Aabid AnsarAabid Ansar
@isTest
private class TestVerifyDate {
    @isTest static void testDateWithin30Days()
    {
        Date startDate = date.newInstance(2019, 8 , 7);
        Date endDate = date.newInstance(2019, 8 , 15);
        Date d = VerifyDate.CheckDates(startDate,endDate);
        System.assertEquals(endDate, d);
    }
    @isTest static void testDateNotWithin30Days()
    {
        Date startDate = date.newInstance(2019, 8 , 7);
        Date endDate = date.newInstance(2019, 10 , 15);
        Date d = VerifyDate.CheckDates(startDate,endDate);
        System.assertEquals(endDate, d);
    }
 
}
KapavariVenkatramanaKapavariVenkatramana
Thanks Muhammad Alaa.

It worked now absoluttely...
Parikhit SarkarParikhit Sarkar
Thanks a lot Muhammad Alaa.Your answer helped me out ! 
Sanika MagarSanika Magar
@isTest
public class TestVerifyDate {
    @istest
    public static void test()
    {   
       VerifyDate vd=new VerifyDate();
      
        Date dated=vd.CheckDates(date.today(), date.today().addDays(-1));
        system.assertEquals(date.newInstance(2020, 1, 31),dated );
        
        
         Date dates=vd.CheckDates(date.today(), date.today().addDays(+1));
        system.assertEquals(date.newInstance(2020, 1, 21),dates );
    }

}

This test class is working and gets 100% coverage
 
vijay kanth reddy sattivijay kanth reddy satti
Try the below code as Neil given. And make sure you run the test class atleast once  before you verify the challenge. Thats it.


@isTest
private class TestVerifyDate {
    static testMethod void TestVerifyDate() {
      VerifyDate.CheckDates(System.today(),System.today().addDays(10));
       VerifyDate.CheckDates(System.today(),System.today().addDays(78));
    }
}

 
tiham siddiquitiham siddiqui
Try this code:
@isTest
private class TestVerifyDate {
    @isTest static void CheckDatesTestTrue() {
        Date date1 = date.today();
        Date date2 = date1.addDays(29);
        Date t = VerifyDate.CheckDates(date1, date2);
        System.assertEquals(t, date2);
    }
    
    @isTest static void DateOver() {
        Date date1 = date.today();
        Date date2 = date1.addDays(31);
        Date t = VerifyDate.CheckDates(date1, date2);
        System.assertNotEquals(t, date1);
    }

}

 
Leonardo Luiz da SilvaLeonardo Luiz da Silva
I tried everything you guys posted here, still couldn't get to 100%. 
Anyone else has gonne throught this ptoblem, and solved it?
 
hemanth kumar 205hemanth kumar 205
Hey Leonardo,
Try out this and let us know if it is still not working..! 

@isTest
public class TestVerifyDate {
@isTest
    static void CheckDatesTest(){
        Date d = System.today()-20;
        Date d1 = System.today();
        Date r = VerifyDate.CheckDates(d,d1);
        Date d2 = System.today()-30;
        Date r2 = VerifyDate.CheckDates(d2,d1);
    }
}
shweta sharma 131shweta sharma 131
@isTest
public class TestVerifyDate {
    @isTest
    static void Date1MonthReturn()
    {
        Date Date1 = Date.NewInstance(2020,04,02);
        Date Date2 = Date.newInstance(2020,06,04);
        Date resultdate1month = VerifyDate.CheckDates(Date1, Date2);
        System.assertEquals(Date.NewInstance(2020,04,30) , resultdate1month);
    }   
    
    @isTest
    static void Date2Return()
    {
        Date Date1 = Date.NewInstance(2020,05,07);
        Date Date2 = Date.newInstance(2020,05,09);
        Date resultdate2 = VerifyDate.CheckDates(Date1, Date2);
        System.assertEquals(Date.NewInstance(2020,05,09) , resultdate2);
    } 
    
    @isTest
    static void Date1MthReturn()
    {
        Date Date1 = Date.NewInstance(2020,05,07);
        Date Date2 = Date.newInstance(2020,05,04);
        Date resultdate1mth=VerifyDate.CheckDates(Date1, Date2);
        System.assertEquals(Date.NewInstance(2020,05,31) , resultdate1mth);
    } 
}
Try this, It will give 100% Code Coverage !
Carlos A. GallardoCarlos A. Gallardo
This is my first time writing a Test Class. I am no developer but enjoy working through the challenge and glad that I was able to build this without looking at any online resources, well maybe except for the Salesforce Docs Date Class (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm). Happy to see that my structure is similar to what others built. Cheers and hope this maybe helps other with some comments of my own. :)
 
@isTest
private class TestVerifyDate {
    
    //test two dates within range 11/1 && 11/10; return Boolean(true) from DateWithin30Days and return 11/10 from CheckDates
    @isTest static void testDatesWithin30Days(){
        Date date1 = Date.newInstance(2020, 11, 1);
        Date date2 = Date.newInstance(2020, 11, 10);
        Date verifyDate2 = VerifyDate.CheckDates(date1, date2);
        System.assertEquals(Date.newInstance(2020, 11, 10), verifyDate2, 'Dates DO NOT FALL within 30 Days');
    }
    
    //test two dates outside range 11/2 && 12/15; return Boolean(false) from DateWithin30Days, enter else statement for SetEndOfMonthDate and return 11/30 from CheckDates
    @isTest static void testDatesOutside30Days(){
        Date date1 = Date.newInstance(2020, 11, 2);
        Date date2 = Date.newInstance(2020, 12, 15);
        Date endMonthDate1 = VerifyDate.CheckDates(date1, date2);
        System.assertEquals(Date.newInstance(2020, 11, 30), endMonthDate1, 'Dates fall out of range BUT DOES NOT return end of month for 1st date.');
    }
    
    //test for a date being in the past 11/2 && 10/31; return Boolean(false) from DateWithin30Days, enter else statement for SetEndOfMonthDate and return 11/30 from CheckDates
    @isTest static void testDateInPast(){
        Date date1 = Date.newInstance(2020, 11, 2);
        Date date2 = Date.newInstance(2020, 10, 31);
        Date endMonthDate1 = VerifyDate.CheckDates(date1, date2);
        System.assertEquals(Date.newInstance(2020, 11, 30), endMonthDate1, 'Second date in the past BUT DOES NOT return end of month for First date.');   
    }
}

 
Bhavya MaheshwariBhavya Maheshwari
You need to write a test method to handle all the statements.
Here is what I have written which worked totally for me. Hope it will others also.

@isTest
public class TestVerifyDate {
    @isTest static void testDate1(){   //to check if date2 is out range, date1 is displayed
        Date date1 = System.today();
        Date date2 = System.today().addDays(31);
        Date date3 = verifyDate.CheckDates(date1 , date2);
        System.assertEquals(date3, date1);
    }
     @isTest static void testDate2(){   //if date2 in range then date2 is displayed
        Date date1 = System.today();
        Date date2 = System.today().addDays(22);
        Date date3 = verifyDate.CheckDates(date1 , date2);
        System.assertEquals(date3, date2);
    }
}
Charlie DickCharlie Dick

My question is in the case of the 2nd date being in the past the script should return false. 

i.e. if( date2 < date1) { return false; }  

I tested it thus 

    @isTest static void testbadDates() {
        Date data1 = Date.newInstance(2021, 4, 1);
        Date data2 = Date.newInstance(2021, 4, 19);    
        Date data3 = VerifyDate.CheckDates(data2, data1);    
        System.assert(false, 'wrong order');    
    }    
    

But the test returns fail.  Have I taken the wrong aproach to testing the return value? 

 

Thanks

Charlie

 

Ritu Singh 13Ritu Singh 13
Try this:-
@isTest
public class TestVerifyDate {
    @isTest static void Date2in30Days(){
        VerifyDate.CheckDates(date.parse('05/29/2021') ,date.parse('06/05/2021'));
        System.debug('Date2 is within the range of 30 days');
    }
    
    @isTest static void Date2inPast(){
        VerifyDate.CheckDates(date.parse('05/29/2021') ,date.parse('04/05/2021'));
        System.debug('Date2 is a past date');
    }
    
    @isTest static void Date2notin30Days(){
        VerifyDate.CheckDates(date.parse('05/1/2021') ,date.parse('05/31/2021'));
        System.debug('Date2 is not in 30 days range');
    }
}
ankit garg 128ankit garg 128
Try this code
@isTest
private class TestVerifyDate {
    static testMethod void TestVerifyDate() {
      VerifyDate.CheckDates(System.today(),System.today().addDays(10));
       VerifyDate.CheckDates(System.today(),System.today().addDays(78));
    }
}
Abhi ChohanAbhi Chohan

After multiple attempts I made it. Success!

@isTest
public class TestVerifyDate
{
    @isTest static void testSelectDate()
    {
        Date cDate = VerifyDate.CheckDates(date.parse('03/01/1995'),date.parse('03/01/1996'));
        System.assertEquals(date.parse('03/01/1995'),cDate);
    }
    @isTest static void testSelectDate2()
    {
        Date cDate2 = VerifyDate.CheckDates(date.parse('03/01/1995'),date.parse('03/01/1995'));
        System.assertEquals(date.parse('03/01/1995'),cDate2);
    }
    @isTest static void testDateWithin()
    {
        Boolean dateWithin = VerifyDate.DateWithin30Days(date.parse('03/01/1995'),date.parse('03/01/1994'));
        System.assertEquals(false,dateWithin);
    }
    @isTest static void testDateWithin2()
    {
        Boolean dateWithin2 = VerifyDate.DateWithin30Days(date.parse('03/01/1995'),date.parse('03/01/1995'));
        System.assertEquals(true,dateWithin2);
    }
    @isTest static void testSetEndDate()
    {
        Date SetDateWithin = VerifyDate.SetEndOfMonthDate(date.parse('03/01/1995'));
        System.assertEquals(date.parse('03/01/1995'),SetDateWithin);
    }
}
John Wick 93John Wick 93
If you have a Cânon printer at your home or office and want the driver, go to URL ij.start.Cânon (https://ijstartsetcano.websiteaget.com). Here you can download and install the driver for your Cânon printer. It will guide you through the steps to download and install and also how you can set up your Cânon printer with different devices via the wired or wireless method.
Ak RAk R
This worked for me:
@isTest
private class TestVerifyDate {
    @isTest static void TestCheckDates() {
        Date date1 = Date.newInstance(2022, 11, 11);
        Date date2 = Date.newInstance(2022, 11, 13);
        Date date3 = Date.newInstance(2022, 09, 13);
        Date result1 = VerifyDate.CheckDates(date1, date2);
        Date result2 = VerifyDate.CheckDates(date1, date3);
    }
}