Mocking static methods with Mockito

Use PowerMockito on top of Mockito. Example code: @RunWith(PowerMockRunner.class) @PrepareForTest(DriverManager.class) public class Mocker { @Test public void shouldVerifyParameters() throws Exception { //given PowerMockito.mockStatic(DriverManager.class); BDDMockito.given(DriverManager.getConnection(…)).willReturn(…); //when sut.execute(); // System Under Test (sut) //then PowerMockito.verifyStatic(); DriverManager.getConnection(…); } More information:

How do I test a class that has private methods, fields or inner classes?

Update: Some 10 years later perhaps the best way to test a private method, or any inaccessible member, is via @Jailbreak from the Manifold framework. @Jailbreak Foo foo = new Foo(); // Direct, *type-safe* access to *all* foo’s members foo.privateMethod(x, y, z); foo.privateField = value; This way your code remains type-safe and readable. No design compromises, no overexposing methods … Read more