I\'ve written a factory to produce java.sql.Connection
objects:
public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory
Mocking of static methods in Mockito is possible since Mockito 3.4.0. For more details see:
https://github.com/mockito/mockito/tree/v3.4.0
https://github.com/mockito/mockito/issues/1013.
In your case, something like this:
@Test
public void testStaticMockWithVerification() throws SQLException {
try (MockedStatic dummy = Mockito.mockStatic(DriverManager.class)) {
DatabaseConnectionFactory factory = new MySQLDatabaseConnectionFactory();
dummy.when(() -> DriverManager.getConnection("arg1", "arg2", "arg3"))
.thenReturn(new Connection() {/*...*/});
factory.getConnection();
dummy.verify(() -> DriverManager.getConnection(eq("arg1"), eq("arg2"), eq("arg3")));
}
}
NOTE: this feature requires mockito-inline dependency.