【发布时间】:2020-12-04 14:03:19
【问题描述】:
大家好,我是 spock 的新手,我在为 save 方法和 setDefaultFormFieldConfig() 编写测试时遇到问题。你可以帮帮我吗?我不知道我的测试出了什么问题。
public ForConfig create(ForConfig forConfig, Long id) {
setNormal(forConfig, "title", "offer.label.title", FieldType.IN);
setNormal(forConfig, "shortDescription", "offer.label.shortDescription", FieldType.TEXTAREA);
forConfig.setCategory(categService.findById(id));
return forConRep.save(formConfig);
}
private void setNormal(FormConfig forConfig, String n, String s, FieldType fieldType) {
if (formConfig.getFc().stream()
.noneMatch(fieldConfig -> fieldConfig.getName().equals(name))) {
forConfig.getFieldsConfig()
.add(new FieldConfig(n, s, null, "M",
fieldType, false, false, true, null));
}
测试保存方法:
def 'test create forConfig without normal fields'() {
given:
def forConfig = Mock(FormConfig)
forConfig.getFieldsConfig() >> new ArrayList<Config>()
def forConfRep = Mock(ForConRep);
def categService = Mock(CategService);
def impl = new ForConfServiceImpl(forConfRep, categService)
forConfRep.save(_) >> new FormConfig()
categoryService.findById(_) >> new Categ()
when:
impl.create(forConfig, 1)
then:
1 * forConfig.getFieldsConfig().add(_)
2 * forConfig.setCategory(_)
}
在控制台中我收到以下错误:
Too few invocations for:
1 * formConfig.getFieldsConfig().add(_) (0 invocations)
Unmatched invocations (ordered by similarity):
None
Too few invocations for:
2 * formConfig.setCategory(_) (1 invocation)
Unmatched invocations (ordered by similarity):
None
at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(InteractionScope.java:98)
at org.spockframework.mock.runtime.MockController.leaveScope(MockController.java:77)
at pl.offer.service.impl.ForConfServiceImplSpec.test save formConfig without defaults fields(FormConfigServiceImplSpec.groovy:24)
Process finished with exit code -1
你能帮帮我吗?
【问题讨论】:
-
欢迎来到 SO。感谢您提出有趣的问题。请注意了解MCVE 是什么以及为什么它对在这里获得合格的帮助非常有帮助:仅仅是因为它使您的问题可以重现。仅仅阅读具有许多依赖项(使用的类和对象)的相当复杂的代码,很难找到问题的根本原因。为了使应用程序和测试类编译,我必须创建许多帮助类,请参阅我的答案。
标签: spring-boot testing groovy spock