我不明白为什么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)
输出: …