Used mockito to replicate SQLException while closing connection to show use of loggedMute

This commit is contained in:
Narendra Pathai
2016-03-15 18:44:59 +05:30
parent adb94044ff
commit 7aff77ab27
3 changed files with 48 additions and 50 deletions

View File

@ -22,6 +22,9 @@
*/
package com.iluwatar.mute;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
@ -46,8 +49,9 @@ public class App {
}
private static void useOfLoggedMute() {
Connection connection = openConnection();
Connection connection = null;
try {
connection = openConnection();
readStuff(connection);
} catch (SQLException ex) {
ex.printStackTrace();
@ -71,7 +75,9 @@ public class App {
}
}
private static Connection openConnection() {
return null;
private static Connection openConnection() throws SQLException {
Connection mockedConnection = mock(Connection.class);
doThrow(SQLException.class).when(mockedConnection).close();
return mockedConnection;
}
}

View File

@ -50,8 +50,8 @@ public final class Mute {
/**
* Executes the <code>runnable</code> and logs the exception occurred on {@link System#err}.
* This method should be utilized to mute the operations about which most you can do is log.
* For instance while closing a connection to database, all you can do is log the exception
* occurred.
* For instance while closing a connection to database, or cleaning up a resource,
* all you can do is log the exception occurred.
*
* @param runnable a runnable that may throw an exception on execution.
*/