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
Mark NemithMark Nemith 

Inputting date parameter into Apex test class

Hi All,

I'm attempting a Trailhead challenge and it requires me to create a test class on a method that takes date parameters. See below:
 
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;
	}

}

...this is my first test method:
 
@istest private class TestVerifyDate {

    @istest static void method1crit1 (){
        Date method1res1 = VerifyDate.CheckDates(12/15/15,12/16/15);
        system.assertEquals(12/16/15,method1res1);
    }
}
...and I am getting the following error in the problems tab of the Dev Console:

"Method does not exist or incorrect signature: VerifyDate.CheckDates(Integer, Integer)"

Please help.

Thanks,

Mark
 
Best Answer chosen by Mark Nemith
pconpcon
You need to generate the date using the Date constructors.
 
@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);
    }
}

All Answers

pconpcon
You need to generate the date using the Date constructors.
 
@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);
    }
}
This was selected as the best answer
SS KarthickSS Karthick

@ Mark Nemith,

The arguments data type  of method CheckDates is date type but in your test class you are passing integer values.Please change the integer to date type as mentioned in below code snippet.

 

@istest
private class TestVerifyDate {
    static testMethod void method1crit1() {
        Date startDate = date.newInstance(2015, 12, 15);
        Date endDate = date.newInstance(2015, 12, 16); 
        Date method1res1 = VerifyDate.CheckDates(startDate, endDate);
        System.assertEquals(endDate, method1res1);
    }
}

 

hope this make sense.
Thanks,
Karthick.S

Mark NemithMark Nemith
Pcon, SS Karthick,

Thanks for both of your answers. Clearly, they are both equally useful. 

It helps to know that I can use both the parse() and newInstance() methods to pass date parameters.

Regards,

Mark