Added a new Spock test file. Updated the POM file to suport Spock.

This commit is contained in:
Chris Jones 2017-02-05 08:17:45 -06:00
parent f09c20793c
commit 12ecbc5c0f
2 changed files with 82 additions and 0 deletions

27
pom.xml
View file

@ -135,6 +135,20 @@
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.1-groovy-2.4-rc-3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -235,6 +249,19 @@
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>

View file

@ -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<ConstraintViolation<Person>> constraintViolations = validator.validate(person);
then:
constraintViolations.size() == 1;
ConstraintViolation<Person> 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).
}