我正在更新 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
?
它必须是 JUnit 规则,因为ResourceTestRule
.