mercredi 5 août 2020

How to mock Calendar.getInstance for Java Testing using Mockito?

I have a piece of code to test -

public class Test {

    public List<Long> getListExpiryTime(long time){
        List<Long> tempList = new ArrayList<Long>();
        tempList.add(getExpiryTime(time));
        return tempList;
    }


    private long getExpiryTime(long time) {
        long currentTime = Calendar.getInstance().getTimeInMillis();
        return time - currentTime / 1000;
    }

}

I want to test whether the List returned has a certain value after I give an input time and we mock Calendar.getInstance().getTimeInMillis();. How exactly do I do this? I prefer to do this using Mockito

Aucun commentaire:

Enregistrer un commentaire