Skip to content

Commit 7aff77a

Browse files
committed
Used mockito to replicate SQLException while closing connection to show use of loggedMute
1 parent adb9404 commit 7aff77a

File tree

3 files changed

+48
-50
lines changed

3 files changed

+48
-50
lines changed

mute-idiom/pom.xml

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,39 @@
11
<?xml version="1.0"?>
2-
<!--
3-
4-
The MIT License
5-
Copyright (c) 2014 Ilkka Seppälä
6-
7-
Permission is hereby granted, free of charge, to any person obtaining a copy
8-
of this software and associated documentation files (the "Software"), to deal
9-
in the Software without restriction, including without limitation the rights
10-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
copies of the Software, and to permit persons to whom the Software is
12-
furnished to do so, subject to the following conditions:
13-
14-
The above copyright notice and this permission notice shall be included in
15-
all copies or substantial portions of the Software.
16-
17-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
THE SOFTWARE.
24-
25-
-->
26-
<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"
27-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
28-
<modelVersion>4.0.0</modelVersion>
29-
<parent>
30-
<groupId>com.iluwatar</groupId>
31-
<artifactId>java-design-patterns</artifactId>
32-
<version>1.11.0-SNAPSHOT</version>
33-
</parent>
34-
<artifactId>mute-idiom</artifactId>
35-
<dependencies>
36-
<dependency>
37-
<groupId>junit</groupId>
38-
<artifactId>junit</artifactId>
39-
<scope>test</scope>
40-
</dependency>
41-
<dependency>
42-
<groupId>org.mockito</groupId>
43-
<artifactId>mockito-core</artifactId>
44-
<scope>test</scope>
45-
</dependency>
46-
</dependencies>
2+
<!-- The MIT License Copyright (c) 2014 Ilkka Seppälä Permission is hereby
3+
granted, free of charge, to any person obtaining a copy of this software
4+
and associated documentation files (the "Software"), to deal in the Software
5+
without restriction, including without limitation the rights to use, copy,
6+
modify, merge, publish, distribute, sublicense, and/or sell copies of the
7+
Software, and to permit persons to whom the Software is furnished to do so,
8+
subject to the following conditions: The above copyright notice and this
9+
permission notice shall be included in all copies or substantial portions
10+
of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
11+
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
13+
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
14+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
16+
DEALINGS IN THE SOFTWARE. -->
17+
<project
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
19+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>com.iluwatar</groupId>
23+
<artifactId>java-design-patterns</artifactId>
24+
<version>1.11.0-SNAPSHOT</version>
25+
</parent>
26+
<artifactId>mute-idiom</artifactId>
27+
<dependencies>
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.mockito</groupId>
35+
<artifactId>mockito-core</artifactId>
36+
<scope>compile</scope>
37+
</dependency>
38+
</dependencies>
4739
</project>

mute-idiom/src/main/java/com/iluwatar/mute/App.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
package com.iluwatar.mute;
2424

25+
import static org.mockito.Mockito.doThrow;
26+
import static org.mockito.Mockito.mock;
27+
2528
import java.io.ByteArrayOutputStream;
2629
import java.sql.Connection;
2730
import java.sql.SQLException;
@@ -46,8 +49,9 @@ private static void useOfMute() {
4649
}
4750

4851
private static void useOfLoggedMute() {
49-
Connection connection = openConnection();
52+
Connection connection = null;
5053
try {
54+
connection = openConnection();
5155
readStuff(connection);
5256
} catch (SQLException ex) {
5357
ex.printStackTrace();
@@ -71,7 +75,9 @@ private static void readStuff(Connection connection) throws SQLException {
7175
}
7276
}
7377

74-
private static Connection openConnection() {
75-
return null;
78+
private static Connection openConnection() throws SQLException {
79+
Connection mockedConnection = mock(Connection.class);
80+
doThrow(SQLException.class).when(mockedConnection).close();
81+
return mockedConnection;
7682
}
7783
}

mute-idiom/src/main/java/com/iluwatar/mute/Mute.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public static void mute(CheckedRunnable runnable) {
5050
/**
5151
* Executes the <code>runnable</code> and logs the exception occurred on {@link System#err}.
5252
* This method should be utilized to mute the operations about which most you can do is log.
53-
* For instance while closing a connection to database, all you can do is log the exception
54-
* occurred.
53+
* For instance while closing a connection to database, or cleaning up a resource,
54+
* all you can do is log the exception occurred.
5555
*
5656
* @param runnable a runnable that may throw an exception on execution.
5757
*/

0 commit comments

Comments
 (0)