Hey guys, I am new in spock and i have problems with writing tests for save method and setDefaultFormFieldConfig(). Could You help me? i don't know what is wrong with my test.
@Override
public FormConfig save(FormConfig formConfig, Long categoryId) {
setDefaultFormFieldConfig(formConfig, "title", "offer.label.title", FieldType.INPUT);
setDefaultFormFieldConfig(formConfig, "shortDescription", "offer.label.shortDescription", FieldType.TEXTAREA);
formConfig.setCategory(categoryService.findById(categoryId));
return formConfigRepository.save(formConfig);
}
private void setDefaultFormFieldConfig(FormConfig formConfig, String name, String label, FieldType type) {
if (formConfig.getFieldsConfig().stream()
.noneMatch(fieldConfig -> fieldConfig.getName().equals(name))) {
formConfig.getFieldsConfig()
.add(new FieldConfig(name, label, null, "MATCH",
type, false, false, true, null));
}
}
Test for save method:
def 'test save formConfig without defaults fields'() {
given:
def formConfig = Mock(FormConfig)
formConfig.getFieldsConfig() >> new ArrayList<FieldConfig>()
def formConfigRepository = Mock(FormConfigRepository);
def categoryService = Mock(CategoryService);
def impl = new FormConfigServiceImpl(formConfigRepository, categoryService)
formConfigRepository.save(_) >> new FormConfig()
categoryService.findById(_) >> new Category()
when:
impl.save(formConfig, 1)
then:
1 * formConfig.getFieldsConfig().add(_)
2 * formConfig.setCategory(_)
}
And in the console I get these errors:
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.anik.technology.book.everything.offer.service.impl.FormConfigServiceImplSpec.test save formConfig without defaults fields(FormConfigServiceImplSpec.groovy:24)
Process finished with exit code -1
Could You help me?
Aucun commentaire:
Enregistrer un commentaire