Thursday, September 22, 2011

And to mock a static void...

Next up, mocking a static void to make it do nothing and then verifiying it was called. Here's how I did it using PowerMock:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Thing.class)
public class MyTest {

   public void test() {

      // mock all static methods, rendering them useless/pointless
      PowerMockito.mockStatic(Thing.class)

      new Thing().save();

      // check save was called
      PowerMockito.verifyStatic();
      Thing.save();

   }

}

No comments:

Post a Comment