Nice, I'm a big fan of mockito, so now we can start using it more in our tests :-)
On Thu, Jul 29, 2010 at 10:04 PM, <[email protected]> wrote: > Author: mbenson > Date: Thu Jul 29 21:04:50 2010 > New Revision: 980571 > > URL: http://svn.apache.org/viewvc?rev=980571&view=rev > Log: > use a mock object in test > > Modified: > incubator/bval/trunk/bval-jsr303/pom.xml > > > incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java > incubator/bval/trunk/pom.xml > > Modified: incubator/bval/trunk/bval-jsr303/pom.xml > URL: > http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/pom.xml?rev=980571&r1=980570&r2=980571&view=diff > > ============================================================================== > --- incubator/bval/trunk/bval-jsr303/pom.xml (original) > +++ incubator/bval/trunk/bval-jsr303/pom.xml Thu Jul 29 21:04:50 2010 > @@ -112,6 +112,11 @@ > <scope>test</scope> > </dependency> > <dependency> > + <groupId>org.mockito</groupId> > + <artifactId>mockito-core</artifactId> > + <scope>test</scope> > + </dependency> > + <dependency> > <groupId>log4j</groupId> > <artifactId>log4j</artifactId> > <scope>test</scope> > > Modified: > incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java > URL: > http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java?rev=980571&r1=980570&r2=980571&view=diff > > ============================================================================== > --- > incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java > (original) > +++ > incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java > Thu Jul 29 21:04:50 2010 > @@ -20,21 +20,17 @@ package org.apache.bval.jsr303; > > import junit.framework.Assert; > import junit.framework.TestCase; > -import org.apache.bval.jsr303.groups.Group; > -import org.apache.bval.jsr303.groups.Groups; > import org.apache.bval.jsr303.util.PathImpl; > -import org.apache.bval.model.MetaBean; > -import org.apache.bval.model.MetaProperty; > import org.apache.bval.model.ValidationListener; > import org.apache.bval.model.ValidationListener.Error; > -import org.apache.bval.util.AccessStrategy; > +import org.mockito.Mock; > +import org.mockito.Mockito; > +import org.mockito.MockitoAnnotations; > +import org.mockito.invocation.InvocationOnMock; > +import org.mockito.stubbing.Answer; > > -import javax.validation.ConstraintValidator; > import javax.validation.ConstraintValidatorContext; > import > javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder; > -import javax.validation.MessageInterpolator; > -import javax.validation.TraversableResolver; > - > > /** > * Checks to validate the correct implementation of > @@ -47,190 +43,71 @@ public class ConstraintValidatorContextT > private ConstraintValidatorContextImpl cvc; > private ConstraintViolationBuilder cvb; > > - private void resetConstraintValidatorContext() { > - this.cvc = new ConstraintValidatorContextImpl( > - new DummyContext<ValidationListener>(), null); > + @Mock > + private GroupValidationContext<ValidationListener> > groupValidationContext; > + > + /** > + * {...@inheritdoc} > + */ > + @Override > + public void setUp() throws Exception { > + super.setUp(); > + MockitoAnnotations.initMocks(this); > + > Mockito.when(groupValidationContext.getPropertyPath()).thenAnswer(new > Answer<PathImpl>() { > + > + public PathImpl answer(InvocationOnMock invocation) throws > Throwable { > + return PathImpl.createPathFromString(""); > + } > + }); > + this.cvc = new > ConstraintValidatorContextImpl(groupValidationContext, > + null); > this.cvc.disableDefaultConstraintViolation(); > this.cvb = > cvc.buildConstraintViolationWithTemplate("dummy.msg.tpl"); > } > > - // Test that builds a path and checks it against the expected one > - public void testPathBuilding() { > - > - resetConstraintValidatorContext(); > - > - // persons[1] > + public void testPerson1() { > cvb.addNode("person").addNode(null).inIterable().atIndex(1) > .addConstraintViolation(); > Error error = cvc.getErrorMessages().iterator().next(); > PathImpl errorPath = (PathImpl) error.getOwner(); > - Assert.assertEquals("Incorrect path created", "person[1]", > errorPath.toString()); > - > - resetConstraintValidatorContext(); > + Assert.assertEquals("Incorrect path created", "person[1]", > errorPath > + .toString()); > + } > > - // persons[lawyer].name > + public void testPersonLawyerName() { > cvb.addNode("person").addNode("name").inIterable().atKey("john") > .addConstraintViolation(); > - error = cvc.getErrorMessages().iterator().next(); > - errorPath = (PathImpl) error.getOwner(); > - Assert.assertEquals("Incorrect path created", "person[john].name", > errorPath.toString()); > - > - resetConstraintValidatorContext(); > + Error error = cvc.getErrorMessages().iterator().next(); > + PathImpl errorPath = (PathImpl) error.getOwner(); > + Assert.assertEquals("Incorrect path created", "person[john].name", > + errorPath.toString()); > + } > > - // [0].name[] > + public void test0Name() { > > cvb.addNode(null).addNode("name").inIterable().atIndex(0).addNode(null) > .inIterable().addConstraintViolation(); > - error = cvc.getErrorMessages().iterator().next(); > - errorPath = (PathImpl) error.getOwner(); > - Assert.assertEquals("Incorrect path created", "[0].name[]", > errorPath.toString()); > - > - resetConstraintValidatorContext(); > + Error error = cvc.getErrorMessages().iterator().next(); > + PathImpl errorPath = (PathImpl) error.getOwner(); > + Assert.assertEquals("Incorrect path created", "[0].name[]", > errorPath > + .toString()); > + } > > - // [] > + public void testEmptyIndex() { > > cvb.addNode(null).addNode(null).inIterable().addConstraintViolation(); > - error = cvc.getErrorMessages().iterator().next(); > - errorPath = (PathImpl) error.getOwner(); > - Assert.assertEquals("Incorrect path created", "[]", > errorPath.toString()); > - > - resetConstraintValidatorContext(); > - > + Error error = cvc.getErrorMessages().iterator().next(); > + PathImpl errorPath = (PathImpl) error.getOwner(); > + Assert.assertEquals("Incorrect path created", "[]", errorPath > + .toString()); > + } > + > + public void testRootPath() { > // Adding only nulls should still give a root path > - > > cvb.addNode(null).addNode(null).addNode(null).addNode(null).addConstraintViolation(); > - error = cvc.getErrorMessages().iterator().next(); > - errorPath = (PathImpl) error.getOwner(); > - Assert.assertTrue("Created path must be a root path", > errorPath.isRootPath()); > - > - } > - > - // TODO: mock > - public static class DummyContext<T> implements > - GroupValidationContext<T> { > - > - public boolean collectValidated(ConstraintValidator constraint) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public ConstraintValidation getConstraintValidation() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public Group getCurrentGroup() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public Groups getGroups() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public MessageInterpolator getMessageResolver() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public PathImpl getPropertyPath() { > - return PathImpl.createPathFromString(""); > - } > - > - public MetaBean getRootMetaBean() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public TraversableResolver getTraversableResolver() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public Object getValidatedValue() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void setConstraintValidation(ConstraintValidation > constraint) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void setCurrentGroup(Group group) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void setFixedValue(Object value) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public boolean collectValidated() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public AccessStrategy getAccess() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public Object getBean() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public ConstraintValidationListener<T> getListener() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public MetaBean getMetaBean() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public MetaProperty getMetaProperty() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public String getPropertyName() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public Object getPropertyValue() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public Object getPropertyValue(AccessStrategy access) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void moveDown(MetaProperty prop, AccessStrategy access) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void moveUp(Object bean, MetaBean metaBean) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void setBean(Object bean) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void setBean(Object aBean, MetaBean aMetaBean) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void setCurrentIndex(Integer index) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void setCurrentKey(Object key) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void setMetaBean(MetaBean metaBean) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - public void setMetaProperty(MetaProperty metaProperty) { > - throw new IllegalStateException("Unexpected call"); > - } > - > - // @Override - not allowed in 1.5 for Interface methods > - public Class<?> getCurrentOwner() { > - throw new IllegalStateException("Unexpected call"); > - } > - > - // @Override - not allowed in 1.5 for Interface methods > - public void setCurrentOwner(Class<?> currentOwner) { > - throw new IllegalStateException("Unexpected call"); > - } > + cvb.addNode(null).addNode(null).addNode(null).addNode(null) > + .addConstraintViolation(); > + Error error = cvc.getErrorMessages().iterator().next(); > + PathImpl errorPath = (PathImpl) error.getOwner(); > + Assert.assertTrue("Created path must be a root path", errorPath > + .isRootPath()); > > } > > > Modified: incubator/bval/trunk/pom.xml > URL: > http://svn.apache.org/viewvc/incubator/bval/trunk/pom.xml?rev=980571&r1=980570&r2=980571&view=diff > > ============================================================================== > --- incubator/bval/trunk/pom.xml (original) > +++ incubator/bval/trunk/pom.xml Thu Jul 29 21:04:50 2010 > @@ -322,7 +322,7 @@ > <artifactId>bval-tck-runner</artifactId> > <version>${project.version}</version> > </dependency> > - > + > <!-- Default of Apache Geronimo version of the Spec API --> > <dependency> > <groupId>org.apache.geronimo.specs</groupId> > @@ -347,6 +347,11 @@ > <version>3.8.2</version> > </dependency> > <dependency> > + <groupId>org.mockito</groupId> > + <artifactId>mockito-core</artifactId> > + <version>1.8.5</version> > + </dependency> > + <dependency> > <groupId>commons-lang</groupId> > <artifactId>commons-lang</artifactId> > <version>2.4</version> > @@ -666,7 +671,7 @@ > <module>bval-core</module> > <module>bval-xstream</module> > <module>bval-jsr303</module> > - <module>bundle</module> > + <module>bundle</module> > <module>bval-json</module> > <module>bval-guice</module> > <module>bval-tck</module> > > >
