0

I have a below code in my controller class.

String sMonth = String.valueof(dateToday.month());
String sDay = String.valueof(dateToday.day());
if(sMonth.length()==1){
    sMonth = '0' + sMonth;
}
if(sDay.length()==1){
    sDay = '0' + sDay;
}

Now, to cover the lines inside if condition my system date's month and day should be of single digit. So, i want to set the system dateToday as something (like 1/1/2012)which satisfy the condition for any day run of test class.

Is it possible to do so ?

3
  • How did you set dateToday? Aug 31, 2016 at 8:45
  • I did not set dateToday but would like to do that in test class. So that when the above code is being executed in my controller , it gets the lines under if condition covered for any day the test class is run.
    – Rajeev
    Aug 31, 2016 at 8:47
  • check my answer. Aug 31, 2016 at 8:55

3 Answers 3

4

As you are using date.today() or system.today() to get the date so you can do something like

if(test.isrunningTest())
{
 dateToday = date.newinstance(1960, 2, 2);
}
String sMonth = String.valueof(dateToday.month());
    String sDay = String.valueof(dateToday.day());
    if(sMonth.length()==1){
    sMonth = '0' + sMonth;
    }
    if(sDay.length()==1){
    sDay = '0' + sDay;
    }

It wiill cover your if condition.

There is one more approach make dateToday public variable or a private variable and access it using @Testvisible and set the value outside the method. When you call this method in test class just set the new value.

8
  • Then I will have to put this condition in my controller class right ? May I do something like this in my test class ?
    – Rajeev
    Aug 31, 2016 at 8:57
  • 1
    Ok. :) Thank you. It anyway help me , just was curious to know if that is possible :) .
    – Rajeev
    Aug 31, 2016 at 9:10
  • 1
    Noooo don't recommend Test .isRunningTest(). Kill it! Much better to use a @TestVisible variable that you overwrite when you need a different value.
    – Adrian Larson
    Aug 31, 2016 at 12:59
  • 1
    So just move it to a top level variable if you need that level of control.
    – Adrian Larson
    Aug 31, 2016 at 14:05
  • 1
    Ah. But see, those are very easy to gloss over (Exhibit A), and you didn't add it to your answer.
    – Adrian Larson
    Aug 31, 2016 at 15:52
1

Simply create a Utility class property

//  -----------------------------------------------
//  today       : getter/setter to have testemthod-driven different values for today
//  -----------------------------------------------
public static Date today        {get {return today == null ? Date.today() : today;} set;}

then, in your code, replace all uses of Date.today() with Util.today

Your test methods can then do

Util.today = Date.newInstance(1960,3,1);  // set test date
// invoke code under test (like the controller)
3
  • I would just use @TestVisible static Date today = Date.today(); personally.
    – Adrian Larson
    Aug 31, 2016 at 15:56
  • @AdrianLarson - for a localized case, sure -- but I like the system-wide solution that can coordinate multiple classes in a complex chain of method invocations
    – cropredy
    Aug 31, 2016 at 16:56
  • I guess so, but why the get/set here? Seems more kludgy than simple assignment.
    – Adrian Larson
    Aug 31, 2016 at 16:57
0

How about this?

    String sMonth = String.valueof(dateToday.month());
// Make the value an Integer so you can test it's value
    Integer vSMonth = Integer.valueOf(sMonth);
// If it's greater than 0 but less then 10 get the right digit
    if(vSMonth > 0 && vSMonth) < 10) sMonth = sMonth.right(1);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .