+ Start a Discussion
Reshmi SmijuReshmi Smiju 

Date argument to a method

Hi,

Pls let me know, How to pass a Date value as an argument to a method.  I am refering the following method in a test class. I tried the following ways. but it showing an error.
1...
@isTest static void check1(){
      Date chk1 = verifyDate.CheckDates('2015-03-08','2015-02-20');
    System.assertEquals('2015-02-20', chk1); }
2...
@isTest static void check1(){
      Date chk1 = verifyDate.CheckDates(2015-03-08,2015-02-20);
    System.assertEquals(2015-02-20, chk1); }
 
AMIT KAMBOJAMIT KAMBOJ
1. Please copy paste the error you get.
2. copy paste the verifydate class with methods.
Reshmi SmijuReshmi Smiju
Hi Amit,
I was doing one of the Trail Head challenges . In that they have provided the following trigger code.

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) {
        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;
}
 }
I need to write the Test Class for the above trigger. And my code as follows..

@isTest
private class TestVerifyDate {

@isTest static void check1(){
      Date chk1 = verifyDate.CheckDates('2015-03-08','2015-02-20');
    System.assertEquals('2015-02-20', chk1); }
@isTest static void check2(){
      Date chk2 = verifyDate.CheckDates('2015-03-08','2015-02-20');
    System.assertEquals('2015-02-28', chk2);}
}
And i am getting the following error on theabove marked(bold characters) line.
Method Does not exist or incorrect signature:verify.CheckDates(String,String)..

The Date value is taken as String. So pls let me know.

Thanks
Reshmi

 
Pierre-AlainPierre-Alain
Hi Reshmi - did you get any answer to your question ?
Thanks.
Gopal P 5Gopal P 5
@Reshmi Smiju, Try using date.ValueOf('2015-03-08').
Juhi JainJuhi Jain
since the functions of VerifyDate class are private,TestVerifyDate will have to inherit is to access its methods.
Satyanarayana GSatyanarayana G
Hi,

Try this:

@isTest
private class TestVerifyDate {

    static testmethod void checkdate1()
    {
        date d1 = date.parse('10/10/15');
        date d2 = date.parse('10/15/15');
        date d = VerifyDate.CheckDates(d1,d2);
        system.assertEquals(d2,d);
    }
    
    static testmethod void checkdate2()
    {
        date d1 = date.parse('10/10/15');
        date d2 = date.parse('11/15/15');
        date d3 = date.parse('10/31/15');
        date d = VerifyDate.CheckDates(d1,d2);
        system.assertEquals(d3,d);
    }
}

Thank you,
Satya
Saumya KoppartySaumya Kopparty
The Date Class provides newInstance(year, month, date) method to construct a date value :
  • exDate myDate = Date.newInstance(2016,01,12);
Use this in your test class to construct new values for date which can then be passed as arguments. Try using the code below (It worked for me!)
 
@isTest
public class TestVerifyDate {
        
    @isTest static void TestcheckDatesWithin30(){
         date d1 = date.newInstance(2016,01,12);
         date d2 = date.newinstance(2016,02,09);
        
        Date dtest = VerifyDate.CheckDates( d1 , d2 );
        system.assertEquals(d2, dtest);
    }
    
    @isTest static void TestDateNotWithtin30(){
        
        date d1 = date.newinstance(2016,01,12);
       date d3 = date.newInstance(2016,02,22);
       date d4endofmonth = date.newinstance(2016,01,31);
        
        Date dtest = VerifyDate.CheckDates(d1, d3);
        system.assertEquals(d4endofmonth, dtest);
        
    }
}
UgNderUgNder
Use this code for dynamic date values:
 
@isTest
public class TestVerifyDate {

    @isTest static void TestCheckDates(){        
        Date D1 = VerifyDate.CheckDates(Date.valueOf(system.today()),Date.valueOf(system.today()).addDays(29));
        system.assertEquals(Date.valueOf(system.today()).addDays(29), D1);
        
        Date D2 = VerifyDate.CheckDates(Date.valueOf(system.today()),Date.valueOf(system.today()).addDays(31));
        system.assertEquals(Date.newInstance(system.today().year(), system.today().month(), Date.daysInMonth(system.today().year(), system.today().month())), D2);        
        
    }
}

 
Swatantra JainSwatantra Jain

Date.parse(String) can be used for parsing the Date to method.
Please note that the date format is 'MM/DD/YYYY'

Following code block gave me 100% code coverage:
 

@isTest
public class TestVerifyDate {
    @isTest static void Test1(){
       Date d = VerifyDate.CheckDates(Date.parse('01/01/1990'), Date.parse('01/03/1990'));
       System.assertEquals(Date.parse('01/03/1990'), d);
    }
    @isTest static void Test2(){
       Date d = VerifyDate.CheckDates(Date.parse('01/01/1990'), Date.parse('02/01/1990'));
       System.assertEquals(Date.parse('01/31/1990'), d);
    }
}

Please mark this answer as correct if it solves your issue.