Testing spock模拟参数匹配失败
我已经编写了一个模拟jersey环境应用程序IsDropWizard的Spock测试。在我的expectations then block中,测试失败,说明调用太少。然而,我在测试类中的sysout显示,正在注册的资源与预期资源中的值相同。我肯定错过了一些很明显的东西,但我看不见 以下是资源定义:Testing spock模拟参数匹配失败,testing,groovy,mocking,spock,Testing,Groovy,Mocking,Spock,我已经编写了一个模拟jersey环境应用程序IsDropWizard的Spock测试。在我的expectations then block中,测试失败,说明调用太少。然而,我在测试类中的sysout显示,正在注册的资源与预期资源中的值相同。我肯定错过了一些很明显的东西,但我看不见 以下是资源定义: @EqualsAndHashCode @ToString public final class ForecastResource { final String defaultState
@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)
很可能ForecastResource没有实现Objectequals,或者没有按照您认为的方式实现它。嗨,彼得-谢谢。我将计数器(AtomicLong的一个实例)作为属性包含在内。一旦我把它转换成一个领域,一切都很好。如果你有动机把它写进一个答案,我会记下来。@BillTurner你可以自己添加一个答案,然后自己接受。这是堆栈溢出中的常见问题。