10

如何使用 PowerMock 模拟 Thread.sleep()?

示例接口和类:

public interface Machine {

    void sleep(long millis);
}

public class MachineImpl
implements Machine {

    private static final Logger logger = Logger.getLogger(MachineImpl.class);

    @Override
    public void sleep(long millis) {
        try {
            if (millis > 0) {
                logger.trace(String.format("Try to sleep for %d millis...", millis));
                Thread.sleep(millis);
            }
        }
        catch (InterruptedException e) {
            logger.trace("Full exception", e);
        }
    }
}
4

2 回答 2

15

这花了我一段时间才弄清楚,所以我正在回答我自己的问题。

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import 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)  // important
@PrepareForTest(MachineImpl.class)  // important: do not use Thread.class here
public class MachineImplTest {

    private MachineImpl classUnderTest;

    @Before
    public void beforeEachTest() {
        classUnderTest = new MachineImpl();
    }

    @Test
    public void sleep_Pass() {
        classUnderTest.sleep(0);
        classUnderTest.sleep(-100);
        classUnderTest.sleep(+100);
    }

    @Test
    public void sleep_Pass2() {
        // We do not want to mock all methods, only specific methods, such as Thread.sleep().
        // Use spy() instead of mockStatic().
        PowerMockito.spy(Thread.class);

        // These two lines are tightly bound.
        PowerMockito.doThrow(new InterruptedException()).when(Thread.class);
        Thread.sleep(Mockito.anyLong());

        classUnderTest.sleep(0);
        classUnderTest.sleep(-100);
        classUnderTest.sleep(+100);
    }
}

如果您使用的是 TestNG,请尝试以下操作:

import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;

@PrepareForTest(MachineImpl.class)  // important: do not use Thread.class here
public class MachineImplTest
extends PowerMockTestCase {
    ...
}

在此处阅读有关 TestNG + Mockito + PowerMock 的更多信息:https ://stackoverflow.com/a/35815153/257299

于 2013-10-21T12:36:59.970 回答
5

这很有帮助。我确实最终使用了一些稍微不同的东西。我使用 EasyMock 而不是 Mockito。

@RunWith(PowerMockRunner.class)
@PrepareForTest({ClassUnderTest.class})
public class MyTest {
    @Before
    public void setUp() {
        PowerMock.mockStatic(Thread.class, methods(Thread.class, "sleep"));
        Thread.sleep(anyLong());
        EasyMock.expectLastCall().anyTimes();
    }
}

无需Thread.class@PrepareForTest.

于 2014-09-18T23:21:35.813 回答