我有一堂课ResolverChainContext:
public class ResolverChainContext {
private AWSCredentialsProvider assumeRoleCredentials;
}
在setup()单元测试(Groovy 和 Spock)中,我用 Mock 填充上面的字段:
def setup() {
def ctx = ResolverChainContext.builder().build()
ctx.setAssumeRoleCredentials(Mock(AWSCredentialsProvider.class))
}
接下来在我的测试用例中,我调用了一个方法getDynamoDBCredentialsV2(),我想v1Credentials从中获取getDynamoDBCredentials()来自 Mock 对象的setup()步骤:
public interface AWSCredentialsProvider {
AWSCredentials getCredentials();
void refresh();
}
protected AWSCredentialsProvider getDynamoDBCredentials() {
final AWSCredentialsProvider fasCredentials = resolverChainContext.getFasCredentials();
final AWSCredentialsProvider roleCredentials = resolverChainContext.getAssumeRoleCredentials();
if (null == fasCredentials || !invokerConfig.isUseCallerCredentials()) {
return roleCredentials;
} else {
return fasCredentials;
}
}
protected AwsCredentialsProvider getDynamoDBCredentialsV2() {
AWSCredentials v1Credentials = getDynamoDBCredentials().getCredentials();
blahblahblah;
}
问题是我会得到null的v1Credentials。
当我调试它时,类型roleCredentials是“Mock for type AWSCredentialsProvider”。getCredentials()在调用它之后,它得到了null。
这是因为我错误地使用了 Groovy 吗?我怎样才能使它v1Credentials成为应该的对象?我应该使用 Mock/Stub 吗?
幕布斯6054654
相关分类