Class HasPropertyWithValue

  • All Implemented Interfaces:
    Constraint, SelfDescribing

    public class HasPropertyWithValue
    extends java.lang.Object
    implements Constraint
    Constraint that asserts that a JavaBean property on an argument passed to the mock object meets the provided constraint. This is useful for when objects are created within code under test and passed to a mock object, and you wish to assert that the created object has certain properties.

    Example Usage

    Consider the situation where we have a class representing a person, which follows the basic JavaBean convention of having get() and possibly set() methods for it's properties: public class Person { private String name; public Person(String person) { this.person = person; } public String getName() { return name; } } And that these person objects are generated within a piece of code under test (a class named PersonGenerator). This object is sent to one of our mock objects which overrides the PersonGenerationListener interface: public interface PersonGenerationListener { public void personGenerated(Person person); } In order to check that the code under test generates a person with name "Iain" we would do the following: Mock personGenListenerMock = mock(PersonGenerationListener.class); personGenListenerMock.expects(once()).method("personGenerated").with(and(isA(Person.class), hasProperty("Name", eq("Iain"))); PersonGenerationListener listener = (PersonGenerationListener)personGenListenerMock.proxy(); If an exception is thrown by the getter method for a property, the property does not exist, is not readable, or a reflection related exception is thrown when trying to invoke it then this is treated as an evaluation failure and the eval method will return false. This constraint class will also work with JavaBean objects that have explicit bean descriptions via an associated BeanInfo description class. See the JavaBeans specification for more information: http://java.sun.com/products/javabeans/docs/index.html
    Since:
    1.1.0
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.StringBuffer describeTo​(java.lang.StringBuffer buffer)
      Appends the description of this object to the buffer.
      boolean eval​(java.lang.Object argument)
      Evaluates the constraint for argument o.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HasPropertyWithValue

        public HasPropertyWithValue​(java.lang.String propertyName,
                                    Constraint expectation)
    • Method Detail

      • eval

        public boolean eval​(java.lang.Object argument)
        Description copied from interface: Constraint
        Evaluates the constraint for argument o.
        Specified by:
        eval in interface Constraint
        Parameters:
        argument - the object against which the constraint is evaluated.
        Returns:
        true if o meets the constraint, false if it does not.
      • describeTo

        public java.lang.StringBuffer describeTo​(java.lang.StringBuffer buffer)
        Description copied from interface: SelfDescribing
        Appends the description of this object to the buffer.
        Specified by:
        describeTo in interface SelfDescribing
        Parameters:
        buffer - The buffer that the description is appended to.
        Returns:
        The buffer passed to the invokedMethod.