How to mock static variables using Mockito

What you will learn here about Mockito

  • How to mock static variables using Mockito

While working with Junit sometimes we come to situation where we need to mock static variable. Here we will see how to mock static variables using Mockito.

How to mock static variables using Mockito

Please follow the following steps to know how to mock static variables using Mockito

1)First create simple maven project

2)Add power mock junit dependency in your pom.xml which is given below
Power mock dependency junit

3)Create a class whose test case you want write and which has private static data members which is shown below
how to mock protected method using mockito

4)Here we are using Whitebox.setInternalState method to set the value for protected static data members which is shown below
How to mock static variables using mockito

You may also like...