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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19494506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:44:58  来源:igfitidea点击:

How to mock Thread.sleep() with PowerMock?

javatestingmockitopowermock

提问by kevinarpe

How to mock Thread.sleep() with PowerMock?

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

Sample interface and class:

示例接口和类:

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);
        }
    }
}

采纳答案by kevinarpe

This took me a while to figure out, so I am answering my own question.

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

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);
    }
}

回答by Dorus

This was helpfull. I did end up using something slightly different. I use EasyMock instead of Mockito.

这很有帮助。我确实最终使用了一些稍微不同的东西。我使用 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();
    }
}

No need to use Thread.classin @PrepareForTest.

无需Thread.class@PrepareForTest.