+ 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...