Implement further exception handling improvements.

This commit is contained in:
baztian
2015-04-10 08:22:03 +02:00
parent a2ba0fb2b1
commit 4ededf479d
4 changed files with 90 additions and 29 deletions
@@ -13,9 +13,20 @@ public abstract class MockConnection implements Connection {
ResultSet mockResultSet;
public final void mockExceptionOnExecute(String exceptionMessage) throws SQLException {
public final void mockExceptionOnCommit(String className, String exceptionMessage) throws SQLException {
Throwable exception = createException(className, exceptionMessage);
Mockito.doThrow(exception).when(this).commit();
}
public final void mockExceptionOnRollback(String className, String exceptionMessage) throws SQLException {
Throwable exception = createException(className, exceptionMessage);
Mockito.doThrow(exception).when(this).rollback();
}
public final void mockExceptionOnExecute(String className, String exceptionMessage) throws SQLException {
PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class);
Mockito.when(mockPreparedStatement.execute()).thenThrow(new SQLException(exceptionMessage));
Throwable exception = createException(className, exceptionMessage);
Mockito.when(mockPreparedStatement.execute()).thenThrow(exception);
Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement);
}
@@ -33,7 +44,19 @@ public abstract class MockConnection implements Connection {
Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement);
}
private int extractTypeCodeForName(String sqlTypesName) {
public final ResultSet verifyResultSet() {
return Mockito.verify(mockResultSet);
}
private static Throwable createException(String className, String exceptionMessage) {
try {
return (Throwable) Class.forName(className).getConstructor(String.class).newInstance(exceptionMessage);
} catch (Exception e) {
throw new RuntimeException("Couldn't initialize class " + className + ".", e);
}
}
private static int extractTypeCodeForName(String sqlTypesName) {
try {
Field field = Types.class.getField(sqlTypesName);
return field.getInt(null);
@@ -48,8 +71,4 @@ public abstract class MockConnection implements Connection {
}
}
public final ResultSet verifyResultSet() {
return Mockito.verify(mockResultSet);
}
}