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
GtennentGtennent 

Test class won't save and can't be selected for a New Run.

I'm trying to complete the "Getting Started with Apex Unit Tests" module on the trailhead. I got to the challenge and created the test class. I did as it says and did an "All Run" then went to complete the challenge, but I got an error back. I checked to see what tests were run and the class I created doesn't show up. I go to "New Run" and it doesn't show up. I saved the class then closed and reopened the tab only to find this:
public class TestVerifyDate{

}

I retyped my code, saved it (ctrl +c and File > save), copied it, and repeat the steps above. Same thing. When I open the code it comes up blank. Luckily I copied it so I can paste it back in. I closed and reopened the DC window. The tab remembers what I typed, but if I close and reopen it gets wiped. I have logged out and back in; all to no avail. So long story short, what is going on? Why won't my class save anything other than the basic starting info? 

Here's a sample of the code. All of the tests repeat the same format so I won't copy them for the sake of brevity.
 
@isTest
private class TestVerifyDate {

    @isTest static void dateTest11(){
        Date date11 = VerifyDate.CheckDates(07/18/2016, 07/29/2016);
        System.assertEquals(07/29/2016, date11);
    }

//Repeat other tests below. All brackets are closed, all semi-colons are present.
//This follows the TemeratureConverterTest example test class format shown in the module,
//Only changes are variables, methods, etc.

}


 
Best Answer chosen by Gtennent
Anupama SamantroyAnupama Samantroy
Hi Geoff,

You need to pass date to the VerifyDate.CheckDates() in your test class.
@isTest
private class TestVerifyDate {

    @isTest static void dateTest11(){
Date stDt =  date.newinstance(2016, 7, 18);

Date endDt =  date.newinstance(2016, 7, 29);

        Date date11 = VerifyDate.CheckDates(stDt,endDt);
        System.assertEquals(07/29/2016, date11);
    }

}

Thanks
Anupama

All Answers

Anupama SamantroyAnupama Samantroy
Hi Geoff,

I would suggest can you go to setup>develop>classes and then create the test class there. Might be a quick fix.

Thanks
Anupama
GtennentGtennent
Thanks Anupama, that helped figure out the problem, which is that I'm getting this error:
"Error: Compile Error: Method does not exist or incorrect signature: VerifyDate.CheckDates(Integer, Integer) at line 5 column 23".

Here's the code provided for the challenge. Full code can be found here:https://github.com/developerforce/trailhead-code-samples/blob/master/VerifyDate.cls
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);
		}
	}
}
In the DC when I type VerifyDate. the option comes up as "CheckDates(Date date1, Date date2): Date - verifydate"

What I don't understand is why the "VerifyDate.CheckDates(Integer, Integer)" error has integers when the CheckDates method seems to require dates.
What am I missing?

 
Anupama SamantroyAnupama Samantroy
Hi Geoff,

You need to pass date to the VerifyDate.CheckDates() in your test class.
@isTest
private class TestVerifyDate {

    @isTest static void dateTest11(){
Date stDt =  date.newinstance(2016, 7, 18);

Date endDt =  date.newinstance(2016, 7, 29);

        Date date11 = VerifyDate.CheckDates(stDt,endDt);
        System.assertEquals(07/29/2016, date11);
    }

}

Thanks
Anupama
This was selected as the best answer
GtennentGtennent
Hi Anupama,

Thanks for the help. What is the reason for this: why is that I can put decimals into unit tests but have to use this method for dates?

Thanks,
Geoff
Anupama SamantroyAnupama Samantroy
Hi Geoff,

The method is defined with parameters as Dates. you cannot put decimals instead even not in your test class.

Thanks
Anupama
GtennentGtennent
Hi Anupama,
I understand that. Let me clarify. In the same module we have the TemperatureConverter class, which has this code: 
public static Decimal FahrenheitToCelsius(Decimal fh)
The test for this uses this code: 
Decimal celsius = TemperatureConverter.FahrenheitToCelsius(70);

My question is, why can I put a number straight into the test when the input is a decimal, but when the input is a date, I have to create another variable to pass a date into the test? In other words why is '70' a valid input for a decimal, but '01/01/2016' isn't a valid input for a date?

Thanks,
Geoff
Anupama SamantroyAnupama Samantroy
Hi Geoff,

Ok now I got your question clearly. 
Salesforce has primitiive datatypes String, Decimal, Integer, Date, DateTime etc. Please go through the link to read more about them https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_primitives.htm.

The method CheckDates has parameters as Dates and when you give the inut as '01/01/2016' it considers this as a string and not a date. Anything which you give within quotes is consideres as date and in salesforce to pass dates you have to create instance of date class.

Thanks 
Anupama