Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerMock Mockito: how to mock all static methods?

Do we need to mock all static methods of a class when using PowerMock (with Mockito)? I mean, suppose we have:

class MockMe {
   public static MockMe getInstance(){
              //return new Instance via complex process;
   }

   public static List<X> anotherStaticMethod(){
      // does xyz
   }
}

My question, if I need to mock getInstance method, is it necessary to mock "anotherStaticMethod" as well?

PowerMock version:1.3, Mockito version:1.8

like image 423
anergy Avatar asked Jan 31 '11 13:01

anergy


1 Answers

No you can use partial mocking using spy in PowerMockito. Or you can use the stubbing API:

stub(method(MockMe.class, "getInstance")).toReturn(myMockMeInstance);
like image 188
Johan Avatar answered Oct 14 '22 11:10

Johan