Mock private method

  1. 在test case 的class级别加注解:@RunWith(PowerMockRunner.class )
  2. 在test case 的class级别加注解:@PrepareForTest(ClassWithPrivateMethod.class)
  3. Use PowerMock.createPartialMock(ClassWithPrivateMethod.class, "nameOfTheMethodToMock") to create a mock object that only mocks the method with name nameOfTheMethodToMock in this class (let's call it mockObject ).
  4. Use PowerMock.expectPrivate(mockObject, "nameOfTheMethodToMock", argument1, argument2) to expect the method call to nameOfTheMethodToMock with arguments argument1 and argument2 .
  5. Use PowerMock.replay(mockObject) to change the mock object to replay mode.
  6. Use PowerMock.verify(mockObject) to change the mock object to verify mode.


在mock private方法时,如果private方法在mockObject的static方法里面,直接调用static方法, mock的


private方法将不会预期执行。

 

你可能感兴趣的:(private)