Mockito要求为课程添加@PrepareForTest,即使在添加@PrepareForTest
我有以下简单的代码。我有一个类(TestClass),我想测试“someMethod”。有一个外部静态方法,由我的“someMethod”调用。我想Powermock那个静态方法给我一些虚拟对象。我在开始时有@PrepareForTest(ExternalClass.class),但是当我执行它时,它会给出错误:
未准备进行测试的类 ExternalClass。若要准备此类,请将类添加到“@PrepareForTest”
批注中。如果您不使用此批注,请在类或方法级别添加批注。
请帮我指出我使用的方式有什么问题@PrepareForTest
@RunWith(PowerMockRunner.class)
@PrepareForTest(ExternalClass.class)
public class xyzTest {
@Mock
private RestTemplate restTemplate;
@Mock
private TestClass testClass;
@BeforeClass
private void setUpBeforeClass() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testSuccessCase() {
Boolean mockResponse = true;
ResponseEntity<Boolean> response = new ResponseEntity<Boolean>(mockResponse, HttpStatus.OK);
SomeClass someClass = new SomeClass("test", "1.0.0", "someUrl", "someMetaData");
PowerMockito.mockStatic(ExternalClass.class);
Mockito.when(restTemplate.postForEntity(any(String.class), any(String.class), eq(Boolean.class))).thenReturn(response);
Mockito.when(ExternalClass.getSomeClass(any(String.class))).thenReturn(someClass);
Boolean result = testClass.someMethod("test");
Assert.isTrue(result);
Mockito.verify(restTemplate, times(1)).postForObject(any(String.class), any(String.class), any());
}
}