You need to sign in to do that
Don't have an account?

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..
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..
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
private class TestVerifyDate {
static testMethod void TestVerifyDate() {
VerifyDate.CheckDates(System.today(),System.today().addDays(10));
VerifyDate.CheckDates(System.today(),System.today().addDays(78));
}
}
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);
}
}
"
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
I don't undestand why the test run in DevCons did not produced the coverage
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);
}
}
If you can't pass this challenge, use the following troubleshooting steps:
- Check the Always Run Asynchronously box as Tim Andrews posted above and run the test.
- 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.
- 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/).
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
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
Try This:
You will just need to change the "date.newInstance(2016, 4, 30)" to the last day of the month when you are running it.
It worked now :)
It worked now :)
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
Simple and easy to understand, use both assert statements
I am getting the following Error :
Method is not visible: VerifyDate.SetEndOfMonthDate(Date)
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
please try this code. I passed the challenge 100% code coverage with this code .
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.
@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));
}
}
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
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) ; }
}
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
Test -> Run All
Check "Overall Code Coverage" should be 100% for this specific Class.
Complete Task
Hunter
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);
}
}
It worked now absoluttely...
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
@isTest
private class TestVerifyDate {
static testMethod void TestVerifyDate() {
VerifyDate.CheckDates(System.today(),System.today().addDays(10));
VerifyDate.CheckDates(System.today(),System.today().addDays(78));
}
}
Anyone else has gonne throught this ptoblem, and solved it?
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);
}
}
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);
}
}
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
@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');
}
}
@isTest
private class TestVerifyDate {
static testMethod void TestVerifyDate() {
VerifyDate.CheckDates(System.today(),System.today().addDays(10));
VerifyDate.CheckDates(System.today(),System.today().addDays(78));
}
}
After multiple attempts I made it. Success!