I\'ve written a factory to produce java.sql.Connection
objects:
public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory
As mention @leokom, since Mockito 3.4.0 you can use the Mockito.mockStatic function to create mocks for static methods. Note that there are patches for this functionality in versions v3.4.2 and v3.4.6, Consider the use of a newer version.
In your case could be 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")));
}
}