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
Complete noviceComplete novice 

Error on code for Trailhead Challenge 'Create a unit test for a simple apex class'

Hi, I'm getting the same error on all the code I try for  the Trailhead Challenge 'Create a unit test for a simple Apex Class'.

The error I get is: Method does not exist or incorrect signature: void CheckDates(Date, Date) from the type VerifyDate

I have tried the following:

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

}


Also:

@isTest
public class TestVerifyDate {
    @isTest static void testVerifyDate(){
        date date1=VerifyDate.CheckDates(system.today(),system.today().addDays(10));
        System.assertEquals(system.today().addDays(10),date1);
        
        date date2=VerifyDate.CheckDates(system.today(),system.today().addDays(78));
        System.assertEquals(system.today(),date2);
    }
}


Also:

@isTest
private class TestVerifyDate{
    @isTest static void testDt1GtrDt2(){
        Date d2 = system.today();
        Date d1 = d2.addDays(10);
        
        Date dt = VerifyDate.CheckDates(d1, d2);
        Date testDt = Date.newInstance(2017, 7, 31);
        System.assertEquals(dt, testDt);
    }
    
    @isTest static void testDt2Within30dayOfDt1(){
        Date d1 = system.today();
        Date d2 = d1.addDays(10);
        
        Date dt = VerifyDate.CheckDates(d1, d2);
        System.assertEquals(dt, d2);
    }
}


This is the code for the Class itself:

public class VerifyDate {
//method to handle potential checks against two dates
public static Date CheckDates(Date date1, Date date2) {
//if date2 is within the next 30 days of date1, use date2. Otherwise use the end of the month
if(DateWithin30Days(date1,date2)) {
return date2;
} else {
return SetEndOfMonthDate(date1);
}
}
//method to check if date2 is within the next 30 days of date1
private static Boolean DateWithin30Days(Date date1, Date date2) {
//check for date2 being in the past
if( date2 < date1) { return false; }
//check that date2 is within (>=) 30 days of date1
Date date30Days = date1.addDays(30); //create a date 30 days away from date1
if( date2 >= date30Days ) { return false; }
else { return true; }
}
//method to return the end of the month of a given date
private static Date SetEndOfMonthDate(Date date1) {
Integer totalDays = Date.daysInMonth(date1.year(), date1.month());
Date lastDay = Date.newInstance(date1.year(), date1.month(), totalDays);
return lastDay;
}
}

Can anyone point me to what I am doing wrong please?

Thanks!




 
Steven NsubugaSteven Nsubuga
Check that you are verifying against the right org. Your first test should work just fine.
Maybe log out of Trailhead, and then log back in, and check that you are verifying the challenge against the right instance.
Raj VakatiRaj Vakati
public class VerifyDate {
	//method to handle potential checks against two dates
	public static Date CheckDates(Date date1, Date date2) {
		//if date2 is within the next 30 days of date1, use date2.  Otherwise use the end of the month
		if(DateWithin30Days(date1,date2)) {
			return date2;
		} else {
			return SetEndOfMonthDate(date1);
		}
	}
	
	//method to check if date2 is within the next 30 days of date1
	private static Boolean DateWithin30Days(Date date1, Date date2) {
		//check for date2 being in the past
        	if( date2 < date1) { return false; }
        
        	//check that date2 is within (>=) 30 days of date1
        	Date date30Days = date1.addDays(30); //create a date 30 days away from date1
		if( date2 >= date30Days ) { return false; }
		else { return true; }
	}
 	//method to return the end of the month of a given date
	private static Date SetEndOfMonthDate(Date date1) {
		Integer totalDays = Date.daysInMonth(date1.year(), date1.month());
		Date lastDay = Date.newInstance(date1.year(), date1.month(), totalDays);
		return lastDay;
	}
}
 
@isTest
private class TestVerifyDate {
    @isTest static void testCheckDates() {
        Date test_date1 = VerifyDate.CheckDates(Date.newInstance(2018, 3, 19), System.today());
        Date test_date2 = VerifyDate.CheckDates(Date.newInstance(2018, 3, 19), System.today() + 100);
        Date test_date3 = VerifyDate.CheckDates(System.today(), System.today()-1);
    }
}

 
Complete noviceComplete novice
Thanks everyone. Weirdly when I checked the challenge it passed so there was clearly some kind of glitch.  This is one of the reasons I find Trailhead a bit difficult!