-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1da33cd
commit a716233
Showing
4 changed files
with
80 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/test/java/org/itsallcode/jdbc/resultset/generic/GenericRowMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.itsallcode.jdbc.resultset.generic; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.lenient; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.sql.*; | ||
|
||
import org.itsallcode.jdbc.UncheckedSQLException; | ||
import org.itsallcode.jdbc.dialect.ColumnValueExtractor; | ||
import org.itsallcode.jdbc.dialect.DbDialect; | ||
import org.itsallcode.jdbc.resultset.generic.GenericRowMapper.ColumnValuesConverter; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class GenericRowMapperTest { | ||
|
||
private static final int ROW_NUM = 42; | ||
@Mock | ||
DbDialect dbDialectMock; | ||
@Mock | ||
ColumnValuesConverter<RowType> converterMock; | ||
@Mock | ||
ResultSet resultSetMock; | ||
@Mock | ||
ResultSetMetaData resultSetMetadataMock; | ||
@Mock | ||
ColumnValueExtractor columnValueExtractorMock; | ||
final RowType expectedRow = new RowType(); | ||
|
||
@Test | ||
void mapRowNoColumn() throws SQLException { | ||
final RowType row = mapRow(0); | ||
assertThat(row).isSameAs(expectedRow); | ||
} | ||
|
||
@Test | ||
void mapRowSuccess() throws SQLException { | ||
final RowType row = mapRow(2); | ||
assertThat(row).isSameAs(expectedRow); | ||
} | ||
|
||
@Test | ||
void mapRowGetObjectFails() throws SQLException { | ||
when(columnValueExtractorMock.getObject(same(resultSetMock), eq(1))).thenThrow(new SQLException("expected")); | ||
assertThatThrownBy(() -> mapRow(2)).isInstanceOf(UncheckedSQLException.class).hasMessage( | ||
"Error extracting value for row 42 / column ColumnMetaData[columnIndex=1, name=null, label=null, type=ColumnType[jdbcType=NULL, typeName=null, className=null, precision=0, scale=0, displaySize=0]]: expected"); | ||
} | ||
|
||
RowType mapRow(final int columnCount) throws SQLException { | ||
when(resultSetMock.getMetaData()).thenReturn(resultSetMetadataMock); | ||
when(resultSetMetadataMock.getColumnCount()).thenReturn(columnCount); | ||
lenient().when(dbDialectMock.createExtractor(any(ColumnMetaData.class))).thenReturn(columnValueExtractorMock); | ||
lenient().when(converterMock.mapRow(any(Row.class))).thenReturn(expectedRow); | ||
return new GenericRowMapper<>(dbDialectMock, converterMock).mapRow(resultSetMock, ROW_NUM); | ||
} | ||
|
||
record RowType() { | ||
} | ||
} |