groovy spock测试模拟和返回的代理
Spock测试-新Spock用户 在模拟返回值方面使用Spock存在问题。 被测类有一个apply方法。第一步是从术语中获取AssetType属性。AssetType和Term都是接口 测试失败,因为似乎没有返回AssetType,而是返回创建的Spock代理groovy spock测试模拟和返回的代理,groovy,Groovy,Spock测试-新Spock用户 在模拟返回值方面使用Spock存在问题。 被测类有一个apply方法。第一步是从术语中获取AssetType属性。AssetType和Term都是接口 测试失败,因为似乎没有返回AssetType,而是返回创建的Spock代理 org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Mock for type 'AssetType' named 'asse
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Mock for type 'AssetType' named 'assetType'' with class 'com.sun.proxy.$Proxy11' to class 'com.collibra.dgc.core.model.meta.AssetType'
public Errors apply(){
def at=t.getType()
...
}
import spock.lang.Specification
import com.collibra.dgc.core.component.representation.TermComponent;
import com.collibra.dgc.core.component.relation.RelationComponent;
import com.collibra.dgc.core.model.representation.Representation
import com.collibra.dgc.core.model.representation.Term;
import com.collibra.dgc.core.api.component.assignment.AssignmentApi
impoort com.collibra.dgc.core.api.internal.dto.instance.view.table.AssetTableOutputRequest
导入com.collibra.dgc.core.api.model.meta.type.AssetType
导入com.collibra.dgc.core.model.representation.representation
class CustomExceptionRuleValidator1Spec extends Specification{
Term term=Mock()
AssetType assetType=Mock()
def """If validate"""() {
given:
RelationComponent relationComponent=Mock();
TermComponent termComponent=Mock();
AssignmentApi assignmentApi=Mock()
ConceptType dataElementConceptType=new DataElementConceptType(assignmentApi)
ConceptType.register(dataElementConceptType)
CustomExceptionRuleValidator classUnderTest=new CustomExceptionRuleValidator(relationComponent,termComponent);
classUnderTest.withTerm(term)
when:
Errors errors=classUnderTest.apply()
then:
1 * term.getType() >> assetType
1 * assetType.getName()>>"Data Element"
1 * term.getSignifier() >> "my data element"
errors.hasErrors()
}
}在类路径中,除了 导入com.collibra.dgc.core.api.model.meta.type.AssetType 这导致了这种行为