0

I have a test with dates that I would like to make dynamic to not have to change them in the future

the correct Json should look like :

private static final String FAKE_RESPONSE_JSON = '[{"locactionName":"Bamboo", "startDate": "2021-07-25", "endDate":"2021-07-29"}]';

To make it more dynamic and test past date I would like to have something like System.today() - 1 but the json ask for a string. So I tried to use String.valueOf(System.today() - 1)

but I get the errors: System.JSONException: Unexpected character (':' (code 58)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [line:1, column:45]

Any idea how I could do it ? thx you

2 Answers 2

0

For the date, try this:

Datetime myDT = Datetime.now().addDays(-1);
String myDTString = myDT.format('yyyy-MM-dd');

For the whole object, you can use a Map.

List<Map<String,String>> myArrayMap = new List<Map<String,String>>();
Map<String,String> myMap = new Map<String,String>();
myMap.put('locationName','Bamboo');
myMap.put('startDate', myDTString);
myMap.put('startDate', myDTString);
myArrayMap.add(myMap);

String FAKE_RESPONSE_JSON = JSON.serialize(myArrayMap);
4
  • Thx you for your answer. I tried Datetime myDT = Datetime.now().addDays(-1); String myDTString = myDT.format('yyyy-MM-dd'); private static final String FAKE_RESPONSE_JSON = '[{"locactionName":"Tel Aviv", "startDate": myDTString, "endDate":"2021-07-29"}]'; But I get System.JSONException: Unexpected character ('m' (code 109)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [line:1, column:45]
    – Ben2pop
    Commented Dec 1, 2022 at 9:07
  • 1
    As a general rule you should turn a Datetime into a Date before subtracting days since doing that to a Datetime actually subtracts 24 hours, not 1 day (this is different when considering daylight savings transitions). In this case it may not matter. Also consider whether you need to provide this from the perspective of a specific time zone. The above will provide yesterday from the perspective of the contextual user, which may not be appropriate.
    – Phil W
    Commented Dec 1, 2022 at 9:09
  • I don't think it really matters in this instance. Commented Dec 1, 2022 at 16:58
  • @Ben2pop Have you tried my exact code? (which I have verified) Looking at your code, you are embedding the variable in the JSON string - but written like that, it's just a string. You would need to concatenate it, ie 'string1' + var + 'string2' Commented Dec 1, 2022 at 17:06
0

It is normally best to hard-code dates in tests and not use e.g. System.today().

Why? As e.g. You Should Use Static Dates For Your Unit Tests explains:

  • Catch Edge Cases Now, Not Later
  • Don’t Disrupt Future Development

This might mean hard coding multiple dates, but at least you will have a determinate view of what works.

(The same argument applies if you are tempted to use a e.g. a large set of random numbers in a test case. Generate the random numbers and then hard code them in the test source code.)

You must log in to answer this question.

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