Spring-Boot 1.4 testing features with Spock
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | |
class ApplicationSpecWithoutAnnotation extends Specification { | |
@Autowired | |
WebApplicationContext context | |
def "should boot up without errors"() { | |
expect: "web application context exists" | |
context != null | |
} | |
} |
buildscript { | |
ext { | |
springBootVersion = '1.4.0.M2' | |
} | |
repositories { | |
mavenCentral() | |
maven { url "https://repo.spring.io/snapshot" } | |
maven { url "https://repo.spring.io/milestone" } | |
} | |
dependencies { | |
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") | |
} | |
} | |
apply plugin: 'groovy' | |
apply plugin: 'spring-boot' | |
jar { | |
baseName = 'spring-boot-spock-testing' | |
version = '0.0.1-SNAPSHOT' | |
} | |
sourceCompatibility = 1.8 | |
targetCompatibility = 1.8 | |
repositories { | |
mavenCentral() | |
maven { url "https://repo.spring.io/snapshot" } | |
maven { url "https://repo.spring.io/milestone" } | |
} | |
dependencies { | |
compile 'org.springframework.boot:spring-boot-starter-data-jpa' | |
compile 'org.springframework.boot:spring-boot-starter-web' | |
runtime 'com.h2database:h2' | |
testCompile 'org.springframework.boot:spring-boot-starter-test' | |
testCompile 'org.spockframework:spock-spring:1.0-groovy-2.4' | |
testCompile 'cglib:cglib-nodep:3.2.2' // for stubbing of classes | |
} | |
task wrapper(type: Wrapper) { | |
gradleVersion = '2.12' | |
} |
@ContextConfiguration | |
@DataJpaTest | |
class HistoricTemperatureDataRepositorySpecIT extends Specification { | |
@Autowired | |
HistoricTemperatureDataRepository historicTemperatureDataRepository | |
@Autowired | |
TestEntityManager testEntityManager | |
def "should load all data"() { | |
given: "one temperature entry" | |
int temperature = 5 | |
HistoricTemperatureData data = new HistoricTemperatureData(temperature, new Timestamp(System.currentTimeMillis())) | |
testEntityManager.persist(data) | |
when: "loading data from repository" | |
def loadedData = historicTemperatureDataRepository.findAll() | |
then: "persisted data is loaded" | |
loadedData.first().temperature == temperature | |
} | |
} |
@Service | |
public class HollandaiseTemperatureMonitor { | |
/** Maximum hollandaise cooking temperature in degree celsius */ | |
private static final int HOLLANDAISE_MAX_TEMPERATURE_THRESHOLD = 80; | |
/** Minimum hollandaise cooking temperature in degree celsius */ | |
private static final int HOLLANDAISE_MIN_TEMPERATURE_THRESHOLD = 45; | |
private final Thermometer thermometer; | |
@Autowired | |
public HollandaiseTemperatureMonitor(Thermometer thermometer) { | |
this.thermometer = thermometer; | |
} | |
public boolean isTemperatureOk() { | |
int temperature = thermometer.currentTemperature(); | |
boolean belowMinimumThreshold = temperature < HOLLANDAISE_MIN_TEMPERATURE_THRESHOLD; | |
boolean aboveMaximumThreshold = temperature > HOLLANDAISE_MAX_TEMPERATURE_THRESHOLD; | |
boolean outOfLimits = belowMinimumThreshold || aboveMaximumThreshold; | |
return !outOfLimits; | |
} | |
} |
class HollandaiseTemperatureMonitorSpec extends Specification { | |
@Unroll | |
def "returns #temperatureOk for temperature #givenTemperature"() { | |
given: "a stub thermometer returning given givenTemperature" | |
Thermometer thermometer = Stub(Thermometer) | |
thermometer.currentTemperature() >> givenTemperature | |
and: "a monitor with the stubbed thermometer" | |
HollandaiseTemperatureMonitor watchman = new HollandaiseTemperatureMonitor(thermometer) | |
expect: | |
watchman.isTemperatureOk() == temperatureOk | |
where: | |
givenTemperature || temperatureOk | |
0 || false | |
100 || false | |
80 || true | |
45 || true | |
60 || true | |
-10 || false | |
} | |
} |
@ContextConfiguration // not mentioned by docs, but had to include this for Spock to startup the Spring context | |
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | |
class SpringBootSpockTestingApplicationSpecIT extends Specification { | |
@Autowired | |
WebApplicationContext context | |
def "should boot up without errors"() { | |
expect: "web application context exists" | |
context != null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
I tried to apply the mock spec always getting a null context, while a JUnit runner was running fine. Eventually I found a suggestion about modifying dependencies and it worked. The dependencies I applied were
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.spockframework:spock-core:1.1-groovy-2.4-rc-1')
testCompile('org.spockframework:spock-spring:1.1-groovy-2.4-rc-1')
See Spring boot context and spock