I'm trying to mock my getParams(), reason of System.currentTimeMillis()
public HashMap<String, String> getParams() {
HashMap<String, String> params = new HashMap<>();
params.put("page[number]", "0");
params.put("page[size]", "20");
params.put("filter[orders][state]", "NEW");
params.put("filter[orders][creationDate][$ge]", String.valueOf(System.currentTimeMillis()-7000)); // 7000 - 10 days
params.put("filter[orders][creationDate][$le]", String.valueOf(System.currentTimeMillis()));
params.put("filter[orders][status]", "APPROVED_BY_BANK");
params.put("filter[orders][signatureRequired]", "false");
params.put("filter[orders][deliveryType]", "DELIVERY");
params.put("include[orders]", "user");
params.put("include[orders]", "entries");
return params;
}
This is my service which sends current time but when i'm trying to test it's generate another time which differs from service's and test. This gives me different result and I can't check if it working fine.
I tried to mock my method, but in the runtime it doesnt mock
RequestService myService = Mockito.spy(requestService);
HashMap<String, String> params = new HashMap<>();
params.put("page[number]", "0");
Mockito
.doReturn(params)
.when(myService).getParams();
//Mockito
//.when(myService.getParams())
// .thenReturn(params);
Any suggestions to fix my problem?
-----------------EDIT I solved my problem with:
public Date truncToSec(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.SECOND, 0);
Date newDate = c.getTime();
return newDate;
Date currnetTime = new Date(System.currentTimeMillis() - 777600000) ;
currnetTime = truncToSec(currnetTime);
params.put("filter[orders][creationDate][$ge]", String.valueOf(currnetTime.toInstant().toEpochMilli())); // 777600000 - 10 days
but if you have better solution you are welcome.