Last active
June 9, 2021 21:22
-
-
Save tstout/99f6286d2533aba314b5c0d2507430d5 to your computer and use it in GitHub Desktop.
Mocking ResultSet #spock #groovy
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
package | |
receipt.service.persistence | |
import org.springframework.jdbc.core.JdbcTemplate | |
import spock.lang.Specification | |
import javax.sql.DataSource | |
import java.sql.* | |
import java.time.LocalDate | |
class FgDataRetrieverTest extends Specification { | |
def resultSet = makeResultSet( | |
["RING_STORE", "ORDER_DATE", "ORDER_END_TIME", "CUSTOMER_NAME_FIRST", "CUSTOMER_NAME_LAST"], | |
["32", testDate(), new Time(99999), "Patty", "O'Furniture"]) | |
def 'processes fg information correctly'() { | |
given: | |
FgDataRetriever retriever = | |
new FgDataRetriever( | |
new JdbcTemplate(dSource(resultSet)), | |
) | |
when: | |
def result = retriever.retrieveFgData('8675309', LocalDate.now(), '131029588') | |
then: | |
result.size() > 0 | |
} | |
Date testDate() { | |
new Date(2020, 2, 3) | |
} | |
Connection connection(rs) { | |
Mock(Connection) { | |
it.prepareStatement(_) >> Mock(PreparedStatement) { | |
it.executeQuery(*_) >> rs | |
} | |
} | |
} | |
DataSource dSource(ResultSet rs) { | |
Mock(DataSource) { | |
it.getConnection() >> connection(rs) | |
} | |
} | |
ResultSet makeResultSet(List<String> aColumns, List... rows) { | |
Mock(ResultSet) { | |
int currentIndex = -1 | |
it.next() >> { ++currentIndex < rows.length } | |
it./get(String|Short|Date|Int|Timestamp|Time)/(_) >> { String argument -> | |
rows[currentIndex][aColumns.indexOf(argument)] | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment