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

@ -1,47 +1,39 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<!-- <!-- The MIT License Copyright (c) 2014 Ilkka Seppälä Permission is hereby
granted, free of charge, to any person obtaining a copy of this software
The MIT License and associated documentation files (the "Software"), to deal in the Software
Copyright (c) 2014 Ilkka Seppälä without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the
Permission is hereby granted, free of charge, to any person obtaining a copy Software, and to permit persons to whom the Software is furnished to do so,
of this software and associated documentation files (the "Software"), to deal subject to the following conditions: The above copyright notice and this
in the Software without restriction, including without limitation the rights permission notice shall be included in all copies or substantial portions
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
copies of the Software, and to permit persons to whom the Software is KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
furnished to do so, subject to the following conditions: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
The above copyright notice and this permission notice shall be included in DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
all copies or substantial portions of the Software. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. -->
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR <project
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER <modelVersion>4.0.0</modelVersion>
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, <parent>
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN <groupId>com.iluwatar</groupId>
THE SOFTWARE. <artifactId>java-design-patterns</artifactId>
<version>1.11.0-SNAPSHOT</version>
--> </parent>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" <artifactId>mute-idiom</artifactId>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <dependencies>
<modelVersion>4.0.0</modelVersion> <dependency>
<parent> <groupId>junit</groupId>
<groupId>com.iluwatar</groupId> <artifactId>junit</artifactId>
<artifactId>java-design-patterns</artifactId> <scope>test</scope>
<version>1.11.0-SNAPSHOT</version> </dependency>
</parent> <dependency>
<artifactId>mute-idiom</artifactId> <groupId>org.mockito</groupId>
<dependencies> <artifactId>mockito-core</artifactId>
<dependency> <scope>compile</scope>
<groupId>junit</groupId> </dependency>
<artifactId>junit</artifactId> </dependencies>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>

View File

@ -22,6 +22,9 @@
*/ */
package com.iluwatar.mute; package com.iluwatar.mute;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
@ -46,8 +49,9 @@ public class App {
} }
private static void useOfLoggedMute() { private static void useOfLoggedMute() {
Connection connection = openConnection(); Connection connection = null;
try { try {
connection = openConnection();
readStuff(connection); readStuff(connection);
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -71,7 +75,9 @@ public class App {
} }
} }
private static Connection openConnection() { private static Connection openConnection() throws SQLException {
return null; 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}. * 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. * 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 * For instance while closing a connection to database, or cleaning up a resource,
* occurred. * all you can do is log the exception occurred.
* *
* @param runnable a runnable that may throw an exception on execution. * @param runnable a runnable that may throw an exception on execution.
*/ */