Setting up Powemockito for static mocking

前端 未结 3 978
感情败类
感情败类 2021-02-08 13:29

I would like to make use of Powermock with Mockito to mock some static method calls. I have followed instructions and examples from SO as well as the PowerMock Getting Started

3条回答
  •  有刺的猬
    2021-02-08 13:59

    If you are using a static mock object, in your PrepareForTest annotation, add the class that is USING the static object in addition to the static class itself. If the class you are testing needs to use this static, add the current class to the annotation. You don't actually mock the class, but it needs to be in the annotation for the static to hook in. It sounds weird, but it works.

    When adding multiple classes into the annotation you can have them inside {} and seperated by commas. For example if your static class is StaticA.class and the class using the static is CallerOfStatic.class you can use:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({StaticA.class, CallerOfStatic.class})
    

提交回复
热议问题