Skip to content

Instantly share code, notes, and snippets.

@tstout
Created December 10, 2019 15:26
Show Gist options
  • Select an option

  • Save tstout/40ef51d8c7bc20c9b0cd0a8d7b8b8329 to your computer and use it in GitHub Desktop.

Select an option

Save tstout/40ef51d8c7bc20c9b0cd0a8d7b8b8329 to your computer and use it in GitHub Desktop.
Mocking DataSource #groovy #spock
package com.containerstore.order.service
import org.joda.time.LocalDate
import org.springframework.jdbc.core.JdbcTemplate
import spock.lang.Specification
import spock.lang.Unroll
import javax.sql.DataSource
import java.sql.Connection
import java.sql.DatabaseMetaData
class SofmToggleLockStateTest extends Specification {
@Unroll
def 'Honor order_services_faux_sofm feature'() {
given:
def jdbcTemplate = jdbc()
def feature = featureService(featureEnabled)
def lock = new SofmToggleLockState(feature, jdbcTemplate)
when:
lock.toggleLock('027372662', LocalDate.now())
then: 'Only call DB procedure when faux feature not enabled'
expectedCallCount * jdbcTemplate.execute(*_)
where:
featureEnabled | expectedCallCount
true | 0
false | 1
}
def featureService(enabled) {
Mock(FeatureService) {
it.isFeatureActiveToday(*_) >> enabled
}
}
def jdbc() {
Mock(JdbcTemplate) {
it.call(*_) >> [:]
it.getDataSource() >> Mock(DataSource) {
it.getConnection() >> Mock(Connection) {
it.getMetaData() >> Mock(DatabaseMetaData)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment