Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking System.currentTimeMillis #1590

Closed
solganik opened this issue Feb 23, 2015 · 5 comments
Closed

Mocking System.currentTimeMillis #1590

solganik opened this issue Feb 23, 2015 · 5 comments
Milestone

Comments

@solganik
Copy link

Hi, I`m using robolectric 2.4 with mockito.
During testing of my App, I need to set time in order to test time specific functionality.
From what i had observed from the sources of robolectric, especially ShadowWrangler class, calls to
System.currentTimeMillis sould be intercepted, and provided via ShadowSystemClock, but it did not work for me.
I had also tried with robolecric and powermockito, but could not make it working, even with
powermock-classloading-xstream.

Thanks.

@pjakubczyk
Copy link

Wrap System.currentTimeMillis to TimeProvider (or any other name) class. Then Mock the provider. much easier

@sasha-stratoscale
Copy link

Hi it would help for source that is under my control, but wont help in case that the call is originated from jars i`m using.

@erd
Copy link
Member

erd commented Feb 23, 2015

From what i had observed from the sources of robolectric, especially ShadowWrangler class, calls to
System.currentTimeMillis sould be intercepted, and provided via ShadowSystemClock, but it did not work for me.

This is true for 3.0, but not 2.4. InstrumentingClassLoader had a bug where the return value of intercepted methods in ShadowWrangler were not being honored.

@erd erd added this to the 3.0 milestone Mar 15, 2015
@erd
Copy link
Member

erd commented Mar 15, 2015

ShadowSystemClock should work correctly in 3.0-SNAPSHOT, so I'm going to close this. If you still have issues after upgrading to 3.0, please re-open.

@erd erd closed this as completed Mar 15, 2015
@tmrudick
Copy link
Contributor

Still having an issue mocking currentTimeMillis with 3.0. The ShadowSystemClock methods are getting called from other shadows but not from my own code under test.

For example:

@Test
public void testCurrentTime() {
    // Calls use ShadowSystemClock
    new Notification();

    // Does not call out to ShadowSystemClock
    mSomeClassUnderTest.doSomethingWithCurrentTime();
}

The first case uses ShadowWrangler as expected. However, in the second case, ShadowWrangler never gets invoked at all.

Any ideas about why this might be happening?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants