From 12ecbc5c0f3fe271c2e589939262afec3f95f495 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Sun, 5 Feb 2017 08:17:45 -0600 Subject: [PATCH] Added a new Spock test file. Updated the POM file to suport Spock. --- pom.xml | 27 +++++++++ .../model/ValidatorSpockTests.groovy | 55 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/test/groovy/org/springframework/samples/petclinic/model/ValidatorSpockTests.groovy diff --git a/pom.xml b/pom.xml index c6f265f68..8e79d2870 100644 --- a/pom.xml +++ b/pom.xml @@ -135,6 +135,20 @@ spring-boot-devtools runtime + + + org.spockframework + spock-core + 1.1-groovy-2.4-rc-3 + test + + + + org.codehaus.groovy + groovy-all + 2.4.7 + test + @@ -235,6 +249,19 @@ + + org.codehaus.gmavenplus + gmavenplus-plugin + 1.5 + + + + compile + testCompile + + + + diff --git a/src/test/groovy/org/springframework/samples/petclinic/model/ValidatorSpockTests.groovy b/src/test/groovy/org/springframework/samples/petclinic/model/ValidatorSpockTests.groovy new file mode 100644 index 000000000..d8c988716 --- /dev/null +++ b/src/test/groovy/org/springframework/samples/petclinic/model/ValidatorSpockTests.groovy @@ -0,0 +1,55 @@ +package org.springframework.samples.petclinic.model; + +//import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Locale; +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validator; + +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; + +import spock.lang.Specification; + +/** + * @author Chris Jones + * Simple tests adapted to use the Spock acceptance test framework. + */ +public class ValidatorSpockTests extends Specification { + + private Validator createValidator() { + LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); + localValidatorFactoryBean.afterPropertiesSet(); + return localValidatorFactoryBean; + } + + // 1. validate against blank first name + def "first name cannot be empty"() { + setup: + def person = new Person(); + def validator = createValidator(); + + when: + person.setFirstName("") + person.setLastName("smith") + Set> constraintViolations = validator.validate(person); + + then: + constraintViolations.size() == 1; + ConstraintViolation violation = constraintViolations.iterator().next(); + violation.getPropertyPath().toString().equals("firstName"); + violation.getMessage().equals("may not be empty"); + } + + // 2. TODO: validate against null first name. + + // 3. TODO: validate a against a valid first name (non-empty, non-null value). + + // 4. TODO: validate against empty last name + + // 5. TODO: validate against null last name + + // 6. TODO: validate a against a valid last name (non-empty, non-null value). +}