小编dep*_*rov的帖子

具有最终类System和静态方法currentTimeMillis的PowerMock

我不明白为什么PowerMock这样做.

测试类

public class Testimpl {
    public long test() {
        long a = System.currentTimeMillis();
        System.out.println("2: " + a);
        return a;
    }
}
Run Code Online (Sandbox Code Playgroud)

JUnit的级

import static org.mockito.MockitoAnnotations.initMocks;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(System.class)
public class TestimplTest {

    @InjectMocks
    Testimpl testimpl;

    @Before
    public void setUp() throws Exception {
        initMocks(testimpl);
        PowerMockito.mockStatic(System.class);
    }

    @Test
    public void test() {
        PowerMockito.when(System.currentTimeMillis()).thenReturn(12345l);
        System.out.println("1: " + System.currentTimeMillis());
        long a = testimpl.test();
        System.out.println("3: " + a);
    }

}
Run Code Online (Sandbox Code Playgroud)

输出: …

java methods static final powermock

4
推荐指数
1
解决办法
4649
查看次数

标签 统计

final ×1

java ×1

methods ×1

powermock ×1

static ×1