Write testcase for #18. (Very rough version. MockConnection method

should be more flexible and use less copy code)
master
baztian 2017-03-12 21:06:12 +01:00
parent 0e482a2052
commit 4df6414d27
3 changed files with 41 additions and 1 deletions

View File

@ -7,6 +7,11 @@
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>

View File

@ -1,28 +1,35 @@
package org.jaydebeapi.mockdriver;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.time.LocalDate;
import org.mockito.Mockito;
public abstract class MockConnection implements Connection {
ResultSet mockResultSet;
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);
Throwable exception = createException(className, exceptionMessage);
@ -30,6 +37,23 @@ public abstract class MockConnection implements Connection {
Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement);
}
public final void mockDateResult(int year, int month, int day) throws SQLException {
PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class);
Mockito.when(mockPreparedStatement.execute()).thenReturn(true);
mockResultSet = Mockito.mock(ResultSet.class, "ResultSet(for date)");
Mockito.when(mockPreparedStatement.getResultSet()).thenReturn(mockResultSet);
Mockito.when(mockResultSet.next()).thenReturn(true);
ResultSetMetaData mockMetaData = Mockito.mock(ResultSetMetaData.class);
Mockito.when(mockResultSet.getMetaData()).thenReturn(mockMetaData);
Mockito.when(mockMetaData.getColumnCount()).thenReturn(1);
Date ancientDate = Date.valueOf(LocalDate.of(year, month, day));
Mockito.when(mockResultSet.getDate(1)).thenReturn(ancientDate);
Mockito.when(mockMetaData.getColumnType(1)).thenReturn(Types.DATE);
Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement);
}
public final void mockType(String sqlTypesName) throws SQLException {
PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class);
Mockito.when(mockPreparedStatement.execute()).thenReturn(true);
@ -44,10 +68,12 @@ public abstract class MockConnection implements Connection {
Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement);
}
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);
@ -56,6 +82,7 @@ public abstract class MockConnection implements Connection {
}
}
private static int extractTypeCodeForName(String sqlTypesName) {
try {
Field field = Types.class.getField(sqlTypesName);

View File

@ -50,6 +50,13 @@ class MockTest(unittest.TestCase):
'getObject'))
verify_get(1)
def test_ancient_date_mapped(self):
self.conn.jconn.mockDateResult(1899, 12, 31)
cursor = self.conn.cursor()
cursor.execute("dummy stmt")
result = cursor.fetchone()
self.assertEquals(result[0], "1899-12-31")
def test_sql_exception_on_execute(self):
self.conn.jconn.mockExceptionOnExecute("java.sql.SQLException", "expected")
cursor = self.conn.cursor()
@ -59,6 +66,7 @@ class MockTest(unittest.TestCase):
except jaydebeapi.DatabaseError as e:
self.assertEquals(str(e), "java.sql.SQLException: expected")
def test_runtime_exception_on_execute(self):
self.conn.jconn.mockExceptionOnExecute("java.lang.RuntimeException", "expected")
cursor = self.conn.cursor()