Spock 无法拦截带有可选参数的方法
Spock failing to intercept method with optional argument(s)
我正在尝试更新 groovy 单元测试,但遇到以下代码问题:
def "getDatasetRecords() returns PaginatedSearchResults with a set of DatasetRecords from the repo"() {
setup:
def mockRecords = []
def originalResults = Mock(PaginatedSearchResults)
def modelMock = Mock(Model) {
isEmpty() >> false
filter(_ as org.matonto.rdf.api.Resource, _ as IRI, _ as Value, null) >>> it // Also tried (*_), (_, _, _) w&w/o Classes
}
def recordMock = Mock(DatasetRecord)
recordMock.getModel() >> modelMock
7.times { mockRecords << recordMock }
originalResults.getPage() >> mockRecords
originalResults.getPageNumber() >> 1
originalResults.getTotalSize() >> 7
originalResults.getPageSize() >> 10
catalogManagerMock.findRecord(*_) >>> originalResults
expect:
def results = service.getDatasetRecords(new DatasetPaginatedSearchParams(vf))
results.getPage().size() == 7
results.getPageSize() == 10
results.getTotalSize() == 7
results.getPageNumber() == 1
}
当我调试代码时:似乎 model.filter
正在返回 null
并且在此处调用 isEmpty()
时抛出 NPE:
public Optional<DatasetRecord> getExisting(Resource resource, Model model, ValueFactory valueFactory, ValueConverterRegistry valueConverterRegistry) {
return (model.filter(resource, valueFactory.createIRI(RDF_TYPE_IRI), this.getTypeIRI()).isEmpty()?Optional.empty():Optional.of(new DatasetRecordImpl(resource, model, valueFactory, valueConverterRegistry)));
}
NPE:
getDatasetRecords() returns PaginatedSearchResults with a set of DatasetRecords from the repo(org.matonto.dataset.impl.SimpleDatasetManagerSpec) Time elapsed: 0.028 sec <<< ERROR!
java.lang.NullPointerException
at org.matonto.dataset.ontology.dataset.DatasetRecordFactory.getExisting(DatasetRecordFactory.java:65)
at org.matonto.rdf.orm.AbstractOrmFactory.getExisting(AbstractOrmFactory.java:159)
at org.matonto.rdf.orm.AbstractOrmFactory.getExisting(AbstractOrmFactory.java:167)
at org.matonto.dataset.pagination.DatasetRecordSearchResults.lambda$new[=12=](DatasetRecordSearchResults.java:46)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:193)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.matonto.dataset.pagination.DatasetRecordSearchResults.<init>(DatasetRecordSearchResults.java:47)
at org.matonto.dataset.impl.SimpleDatasetManager.getDatasetRecords(SimpleDatasetManager.java:155)
at org.matonto.dataset.impl.SimpleDatasetManagerSpec.getDatasetRecords() returns PaginatedSearchResults with a set of DatasetRecords from the repo(SimpleDatasetManagerSpec.groovy:371)
filter
的定义:
Model filter(Resource subject, IRI predicate, Value object, Resource... context);
我已经尝试过在有和没有最后一个参数的情况下完全检查模拟对象的方法签名。如果能为 filter
方法的模拟拦截器提供任何帮助,我们将不胜感激。
试试这个方法:
class Spec extends Specification {
def 'mock returns itself'() {
given:
def mock = Mock(Model) {
filter(*_) >> it
}
expect:
mock.filter(1, 2, 3, 4) == mock
mock.filter(1, 2, 3) == mock
}
}
class Model {
Model filter(a, b, c, ... d) {
new Model()
}
}
我的代码(上面)没问题,但做了一个小改动:
def modelMock = Mock(Model) {
isEmpty() >> false
filter(*_) >> it
}
我的问题原来是 IntelliJ 并试图在那里调试它。
我正在尝试更新 groovy 单元测试,但遇到以下代码问题:
def "getDatasetRecords() returns PaginatedSearchResults with a set of DatasetRecords from the repo"() {
setup:
def mockRecords = []
def originalResults = Mock(PaginatedSearchResults)
def modelMock = Mock(Model) {
isEmpty() >> false
filter(_ as org.matonto.rdf.api.Resource, _ as IRI, _ as Value, null) >>> it // Also tried (*_), (_, _, _) w&w/o Classes
}
def recordMock = Mock(DatasetRecord)
recordMock.getModel() >> modelMock
7.times { mockRecords << recordMock }
originalResults.getPage() >> mockRecords
originalResults.getPageNumber() >> 1
originalResults.getTotalSize() >> 7
originalResults.getPageSize() >> 10
catalogManagerMock.findRecord(*_) >>> originalResults
expect:
def results = service.getDatasetRecords(new DatasetPaginatedSearchParams(vf))
results.getPage().size() == 7
results.getPageSize() == 10
results.getTotalSize() == 7
results.getPageNumber() == 1
}
当我调试代码时:似乎 model.filter
正在返回 null
并且在此处调用 isEmpty()
时抛出 NPE:
public Optional<DatasetRecord> getExisting(Resource resource, Model model, ValueFactory valueFactory, ValueConverterRegistry valueConverterRegistry) {
return (model.filter(resource, valueFactory.createIRI(RDF_TYPE_IRI), this.getTypeIRI()).isEmpty()?Optional.empty():Optional.of(new DatasetRecordImpl(resource, model, valueFactory, valueConverterRegistry)));
}
NPE:
getDatasetRecords() returns PaginatedSearchResults with a set of DatasetRecords from the repo(org.matonto.dataset.impl.SimpleDatasetManagerSpec) Time elapsed: 0.028 sec <<< ERROR!
java.lang.NullPointerException
at org.matonto.dataset.ontology.dataset.DatasetRecordFactory.getExisting(DatasetRecordFactory.java:65)
at org.matonto.rdf.orm.AbstractOrmFactory.getExisting(AbstractOrmFactory.java:159)
at org.matonto.rdf.orm.AbstractOrmFactory.getExisting(AbstractOrmFactory.java:167)
at org.matonto.dataset.pagination.DatasetRecordSearchResults.lambda$new[=12=](DatasetRecordSearchResults.java:46)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:193)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.matonto.dataset.pagination.DatasetRecordSearchResults.<init>(DatasetRecordSearchResults.java:47)
at org.matonto.dataset.impl.SimpleDatasetManager.getDatasetRecords(SimpleDatasetManager.java:155)
at org.matonto.dataset.impl.SimpleDatasetManagerSpec.getDatasetRecords() returns PaginatedSearchResults with a set of DatasetRecords from the repo(SimpleDatasetManagerSpec.groovy:371)
filter
的定义:
Model filter(Resource subject, IRI predicate, Value object, Resource... context);
我已经尝试过在有和没有最后一个参数的情况下完全检查模拟对象的方法签名。如果能为 filter
方法的模拟拦截器提供任何帮助,我们将不胜感激。
试试这个方法:
class Spec extends Specification {
def 'mock returns itself'() {
given:
def mock = Mock(Model) {
filter(*_) >> it
}
expect:
mock.filter(1, 2, 3, 4) == mock
mock.filter(1, 2, 3) == mock
}
}
class Model {
Model filter(a, b, c, ... d) {
new Model()
}
}
我的代码(上面)没问题,但做了一个小改动:
def modelMock = Mock(Model) {
isEmpty() >> false
filter(*_) >> it
}
我的问题原来是 IntelliJ 并试图在那里调试它。