我写了一个模拟球衣环境的Spock测试(应用程序是DropWizard)。在我的期望(然后阻止),测试失败说明调用太少。但是,我测试的类中的sysout显示正在注册的资源与预期资源中的资源具有相同的值。我必须遗漏一些非常明显的东西,但看不到它。
以下是资源定义:
@EqualsAndHashCode
@ToString
public final class ForecastResource {
final String defaultState
final String defaultCity
final String defaultApiKeyLocation
final AtomicLong counter
private final WundergroundClient wundergroundClient
public ForecastResource(final String defaultState, final String defaultCity, final String defaultApiKeyLocation) {
this(defaultState, defaultCity, defaultApiKeyLocation, new WundergroundClient())
}
public ForecastResource(final String defaultState, final String defaultCity, final String defaultApiKeyLocation, final WundergroundClient wundergroundClient) {
this.defaultState = defaultState
this.defaultCity = defaultCity
this.defaultApiKeyLocation = defaultApiKeyLocation
this.wundergroundClient = wundergroundClient
this.counter = new AtomicLong()
}
...
}
以下是测试中的代码:
public void run(ForecastConfiguration configuration, Environment environment) {
final ForecastResource resource = new ForecastResource(
configuration.getDefaultState(),
configuration.getDefaultCity(),
configuration.getDefaultApiKeyLocation()
)
final TemplateHealthCheck healthCheck =
new TemplateHealthCheck(configuration.getDefaultCity())
environment.healthChecks().register("template", healthCheck)
println "registering ${resource.toString()}"
environment.jersey().register(resource)
}
而且,这是我的测试代码:
def "validate run registers the resource"() {
given: "environment has mock jersey and health check implementations"
def mockJerseyEnvironment = Mock(JerseyEnvironment)
def mockHealthCheckRegistry = Mock(HealthCheckRegistry)
def mockEnvironment = Mock(Environment)
mockEnvironment.healthChecks() >> mockHealthCheckRegistry
mockEnvironment.jersey() >> mockJerseyEnvironment
and: "a configuration for portland WA"
ForecastConfiguration configuration = new ForecastConfiguration(defaultState: "WA", defaultCity: "Portland", defaultApiKeyLocation: '/forecast/serviceadapter/test-wunderground.properties')
and: "a resource expected to be registered"
ForecastResource expectedResource = new ForecastResource("WA", "Portland", '/forecast/serviceadapter/test-wunderground.properties')
and: "a health check expected to be registered"
TemplateHealthCheck expectedHealthCheck = new TemplateHealthCheck("Portland")
when: "run is called"
new ForecastApplication().run(configuration, mockEnvironment)
then: "expect resource and health check to be registered"
1 * mockJerseyEnvironment.register(expectedResource)
1 * mockHealthCheckRegistry.register("template", expectedHealthCheck)
}
最后,以下是sysout后跟堆栈跟踪的结果:
registered :registering forecast.resources.ForecastResource(WA, Portland, /forecast/serviceadapter/test-wunderground.properties, 0)
Too few invocations for:
1 * mockJerseyEnvironment.register(expectedResource) (0 invocations)
Unmatched invocations (ordered by similarity):
1 * mockJerseyEnvironment.register(forecast.resources.ForecastResource(WA, Portland, /forecast/serviceadapter/test-wunderground.properties, 0))
at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(InteractionScope.java:78)
at org.spockframework.mock.runtime.MockController.leaveScope(MockController.java:76)
at forecast.ForecastApplicationSpec.validate run registers the resource(ForecastApplicationSpec.groovy:32)