How to mock a final class with mockito

后端 未结 25 1548
日久生厌
日久生厌 2020-11-22 16:35

I have a final class, something like this:

public final class RainOnTrees{

   public void startRain(){

        // some code here
   }
}

I

25条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 16:51

    For final class add below to mock and call static or non static.

    1- add this in class level @SuppressStatucInitializationFor(value ={class name with package})
    2- PowerMockito.mockStatic(classname.class) will mock class
    3- then use your when statement to return mock object when calling method of this class.

    Enjoy

提交回复
热议问题