-------------------- StaticVoidThreadSleep.java --------------------
package omg;
public final class StaticVoidThreadSleep {
public void doSomething() throws InterruptedException {
Thread.sleep(30000);
}
}
------------------------------------------------------------------------------------------
And my test:
-------------------- TestStaticVoidThreadSleep.java --------------------
package omg;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.Mockito.*;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
public final class TestStaticVoidThreadSleep {
@PrepareForTest(Thread.class)
@Test
public void doSomethingQuick() throws Exception {
PowerMockito.mockStatic(Thread.class);
Thread.sleep(anyInt()); // Not sure whether that's the right way
to stub out the call...
StaticVoidThreadSleep t = new StaticVoidThreadSleep();
t.doSomething();
}
}
------------------------------------------------------------------------------------------
My test is still calling the real Thread.sleep():
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at omg.StaticVoidThreadSleep.doSomething(StaticVoidThreadSleep.java:6)
at omg.TestStaticVoidThreadSleep.doSomethingQuick(TestStaticVoidThreadSleep.java:22)
What am I doing wrong? Thanks.
--
Benoit "tsuna" Sigoure
Running on: java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-10M3025)
Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01-101, mixed mode)
s/methid/method/ in the subject :)
Thanks.
--
Benoit "tsuna" Sigoure
Software Engineer @ www.StumbleUpon.com
@RunWith(PowerMockRunner.class)@PrepareForTest(StaticVoidThreadSleep.class)public final class TestStaticVoidThreadSleep {
@Test
public void doSomethingQuick() throws Exception {
mockStatic(Thread.class);doNothing().when(Thread.class);Thread.sleep(anyLong());
StaticVoidThreadSleep t = new StaticVoidThreadSleep();
t.doSomething();
}
}
I've uploaded an example to our subversion as well, see method "mockingStaticVoidMethodWorks".
--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.
OK, that's why I was confused as to why sometimes you need to use
@PrepareForTest(ClassInWhichTheresStaticMethod) and sometimes
@PrepareForTest(ClassBeingTested).
On http://code.google.com/p/powermock/wiki/MockSystem, point #2 says
that the @PrepareForTest annotation has to be used "at the class-level
of the test case". It also works at the method-level.
Would it possible to link the new MockSystem page from
http://code.google.com/p/powermock/wiki/MockitoUsage? It would make
it easier to find that page.
My test works now, thank you so much. PowerMock is awesome!
One minor nit: I noticed that there's a huge spike in CPU usage before
my tests start running. If I remove everything related to PowerMock,
it disappears. Are @PrepareForTest and such doing some expensive
stuff?