我正在将TestNg与PowerMocktion 1.7.4一起使用。 @Spy无法正常工作。 抛出异常
org.mockito.exceptions.base.MockitoException:无法为创建@Spy “服务”字段,因为缺少 instance
正确使用@Spy的示例:
@Spy List mock = new LinkedList();
@PrepareForTest(StaticClass.class)
public class UsingPowerMockTest extends PowerMockTestCase {
@Spy
ServiceTest service;
@BeforeMethod
public void before() throws Exception {
//service = PowerMockito.spy(new ServiceTest());
mockStatic(StaticClass.class);
}
@Test
public void should_mock_static_method() {
PowerMockito.when(StaticClass.randomGenerator(Mockito.anyInt())).thenReturn("asd");
String randomString = service.getRandomString(20);
System.out.println(randomString);
}
}