MockStatic of Unit Test

来源:互联网 发布:平衡面板数据是什么 编辑:程序博客网 时间:2024/05/20 08:01

-- Configure class:

1. Use the @RunWith(PowerMockRunner.class) annotation at the class-level of the test case.
2. Use the @PrepareForTest(ClassThatContainsStaticMethod.class) 3. annotation at the class-level of the test case.
3. Use PowerMock.mockStatic(ClassThatContainsStaticMethod.class) to mock all methods of this class.
4. Use PowerMock.replay(ClassThatContainsStaticMethod.class) to change the class to replay mode.
5. Use PowerMock.verify(ClassThatContainsStaticMethod.class) to change the class to verify mode.


-- Learned:

1. mockStatic(*.class) is to mock all the static methods (even with final) of the class.
2. when(#1).then(#2),#1 is a static method called by a class, and the static method is mocked by -1-.


-- Question:

1. How the PowerMock.replay work and what's the effect?
2. How the PowerMock.verify work and what's the effect?