【发布时间】:2018-08-30 13:25:08
【问题描述】:
我正在更新 Spock 测试。很少有模拟和@Rule 资源:
AuthTokenService mockAuthTokenService = Mock()
ObjectMapper mockObjectMapper = Mock()
GeneralConfiguration conf = Mock();
def CLA_BASE_URL = "http://some-prefix/";
@Rule
ResourceTestRule resource = ResourceTestRule.builder()
.addResource(new AuthResourceImpl(mockAuthTokenService, mockObjectMapper, conf))
.build()
我需要资源为两个不同的测试提供不同的conf。所以我尝试了
def 'create auth token with configured URL prefix'() {
setup:
AuthTokenMetaData authTokenMetaData = buildAuthTokenMetaData()
when:
conf.getClaBaseUrl() >> CLA_BASE_URL
...
但这不起作用,因为resource 被创建一次。
所以我不得不添加另一个资源。
GeneralConfiguration conf2 = new GeneralConfiguration().setClaBaseUrl(CLA_BASE_URL);
@Rule
ResourceTestRule resource2 = ResourceTestRule.builder()
.addResource(new AuthResourceImpl(mockAuthTokenService, mockObjectMapper, conf2))
.build()
但这感觉有点奇怪,而且从与 Spock 的短暂接触中,我相信它有更好的方法来解决这个问题。
如何参数化ResourceTestRule?
由于ResourceTestRule 的底层实现,它必须是 JUnit 规则。
【问题讨论】:
标签: groovy mocking spock junit-rule