You need to sign in to do that
Don't have an account?
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
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
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;
OR you can use some think like this.
Please mark the answer as Best Answer if this is the answer you are looking for.
All Answers
Please mark the answer as Best Answer if it really helped.
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;
OR you can use some think like this.
Please mark the answer as Best Answer if this is the answer you are looking for.
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);
}
}
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);
}
}
private class TestVerifyDate {
static testMethod void TestVerifyDate() {
VerifyDate.CheckDates(System.today(),System.today().addDays(10));
VerifyDate.CheckDates(System.today(),System.today().addDays(78));
}
}
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);
}
}
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.
@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))));
}
}