+ Start a Discussion
Yogesh Dighe.Yogesh Dighe. 

is it correct my apex @Test code.? its gives me error..!!!

Apex class as following-

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;
}
}

-------------------------------------------------
@Test class as following-

@isTest
public class TestVerifyDate
{
     @isTest static void testWarmTemp()
     {
          Date dte = VerifyDate.CheckDates(1,10);
          System.assertEquals(10,dte);
     }
}

--------------------------

test code show me the following error-
[Error] Error: Compile Error: Method does not exist or incorrect signature: VerifyDate.CheckDates(Integer, Integer) at line 6 column 21
Best Answer chosen by Yogesh Dighe.
Mert YALTIMert YALTI


Hi Yogesh;

CheckDates method has 2 parameters. Those parameters are Date type. In your unit test you are trying to pass integer parameters which is the cause of the error. Please change your unit test with the code given below;

@isTest 
private class TestVerifyDate {
    static testMethod void TestVerifyDate() {
        //within 30
        String returnValue=String.valueOf(VerifyDate.CheckDates(Date.newInstance(2015, 2, 15),Date.newInstance(2015, 2, 20)));
            
        //not within 30         
        String returnValue2=String.valueOf(VerifyDate.CheckDates( Date.newInstance(2015, 2, 15) , Date.newInstance(2015, 3, 28) ));
    }
}
 

OR you can use some think like this.

@isTest 
private class TestVerifyDate {
    static testMethod void TestVerifyDate() {
        //within 30
        Date date1=system.today();
        Date date2=system.today().addDays(5);
        String returnValue=String.valueOf(VerifyDate.CheckDates(date1,date2);
            
        //not within 30 
        Date date3=system.today();
        Date date4=system.today().addDays(35);        
        String returnValue2=String.valueOf(VerifyDate.CheckDates(date3,date4);
    }
}
 

 


Please mark the answer as Best Answer if this is the answer you are looking for.
 

All Answers

AshwaniAshwani
Error is correct, you are trying to pass integer in a method which accepts "Date" type as a argument. So you should pass date type instance not the integer.
Yogesh Dighe.Yogesh Dighe.
how can i pass date as argument? i mean, in which format? plz give me a example.. Thanx in Advance :)
Swayam  AroraSwayam Arora
Date dte = VerifyDate.CheckDates(Date.newInstance(2015, 1, 10)); // If the date is 10th January, 2015

Please mark the answer as Best Answer if it really helped.
Mert YALTIMert YALTI


Hi Yogesh;

CheckDates method has 2 parameters. Those parameters are Date type. In your unit test you are trying to pass integer parameters which is the cause of the error. Please change your unit test with the code given below;

@isTest 
private class TestVerifyDate {
    static testMethod void TestVerifyDate() {
        //within 30
        String returnValue=String.valueOf(VerifyDate.CheckDates(Date.newInstance(2015, 2, 15),Date.newInstance(2015, 2, 20)));
            
        //not within 30         
        String returnValue2=String.valueOf(VerifyDate.CheckDates( Date.newInstance(2015, 2, 15) , Date.newInstance(2015, 3, 28) ));
    }
}
 

OR you can use some think like this.

@isTest 
private class TestVerifyDate {
    static testMethod void TestVerifyDate() {
        //within 30
        Date date1=system.today();
        Date date2=system.today().addDays(5);
        String returnValue=String.valueOf(VerifyDate.CheckDates(date1,date2);
            
        //not within 30 
        Date date3=system.today();
        Date date4=system.today().addDays(35);        
        String returnValue2=String.valueOf(VerifyDate.CheckDates(date3,date4);
    }
}
 

 


Please mark the answer as Best Answer if this is the answer you are looking for.
 

This was selected as the best answer
Yogesh Dighe.Yogesh Dighe.
Return type is Date, Not a String.
So, m do changes in my code as follow... :)
Thanx Swayam @ Mert YALTI

@isTest
public class TestVerifyDate
{
     @isTest static void testWarmTemp()
     {
         Date dte = VerifyDate.CheckDates(Date.newInstance(2015, 1, 1), Date.newInstance(2015, 1, 10));
         System.assertEquals(Date.newInstance(2015, 1, 10),dte);
     }
}
Raghu.2020Raghu.2020
Hi Yogesh,

Below is my solution. 

@isTest
private class TestVerifyDate {

    @isTest static void testin(){
        
        date D = verifydate.CheckDates(date.newInstance(2015, 04, 30),date.newInstance(2015, 05, 15));
        system.assertequals(date.newInstance(2015, 05, 15),D);
    }
    
    @isTest static void testout(){
        
        date M = verifydate.CheckDates(date.newInstance(2015, 04, 27),date.newInstance(2015, 06, 15));
        system.assertEquals(date.newInstance(2015, 04, 30), M);
    }
}
PIYUSH  KUMARPIYUSH KUMAR
@isTest
private class TestVerifyDate {
    static testMethod void TestVerifyDate() {
      VerifyDate.CheckDates(System.today(),System.today().addDays(10));
       VerifyDate.CheckDates(System.today(),System.today().addDays(78));
    }
}
Kishan MalepuKishan Malepu
@isTest
private class TestVerifyDate {
    
   @isTest  static void firstMethod()
    {
       Date d = VerifyDate.CheckDates(system.today(),system.today().addDays(20));
        system.assertEquals(system.today().addDays(20), d);
    }
    @isTest static void secondMethod(){
        Date d2 = VerifyDate.CheckDates(system.today(),system.today().addDays(35));
        system.assertEquals(system.today().addDays(28), d2);
    }
    
}
jothiraj gnanaprakasamjothiraj gnanaprakasam
@isTest
private class TestVerifyDate {
    @isTest static void testDateWithin30Days() {
        
        Date d = VerifyDate.CheckDates(system.today(), system.today().addDays(20));
        System.assertEquals(d,system.today().addDays(20));
        
        d = VerifyDate.CheckDates(system.today(), system.today().addDays(40));
        Integer totalDays = Date.daysInMonth(system.today().year(), system.today().month());
        Date lastDay = Date.newInstance(system.today().year(), system.today().month(), totalDays);
        System.assertEquals( lastDay, d);
        
        d = VerifyDate.CheckDates(system.today(), system.today().addDays(-2));
        System.assertEquals(lastDay, d);
        
        
    }
  
    }

Please mark the answer as Best Answer if this is the answer you are looking for.
Sandeep SisodiaSandeep Sisodia
Code Coverage 100%

@isTest(SeeAllData = false)
private class TestVerifyDate
{
    @isTest
    private static void TestVerifyDate()
    {
        //within 30 days
        System.assertEquals(Date.newInstance(2020, 5, 30),(VerifyDate.CheckDates(Date.newInstance(2020, 5, 26), Date.newInstance(2020, 5, 30))));
            
        //not within 30 days         
        System.assertEquals(Date.newInstance(2020, 5, 31),(VerifyDate.CheckDates(Date.newInstance(2020, 5, 26), Date.newInstance(2020, 6, 28))));
        
        //date in past
        System.assertEquals(Date.newInstance(2020, 6, 30),(VerifyDate.CheckDates(Date.newInstance(2020, 6, 26), Date.newInstance(2020, 5, 28))));
    }
}