lundi 27 juillet 2015

Robolectric 3.0: Mocking System.currentTimeMillis()

I'm trying to modify the value returned by System.currentTimeMillis() so I can do operations like: write something to the database, simulate waiting 5 minutes, do a query on the database (the query is dependent on when the value is written).

The code suggested in [this SO thread]:

ShadowSystemClock shadowClock = Robolectric.shadowOf(SystemClock.class);
shadowClock.setCurrentTimeMillis(1424369871446);

Does not compile since the shadowOf method was removed. Trying alternatives like:

ShadowSystemClock shadowClock = new ShadowSystemClock();
shadowClock.setCurrentTimeMillis(1424369871446);

It appears there were issues with overriding currentTimeMillis() but those issues should be fixed as of version 3.0.

I could add PowerMock to my project and use that for this case I think, but if this is do-able with Robolectric, I'd prefer that.

Aucun commentaire:

Enregistrer un commentaire