3.8. Using Exemplars in Interfaces and Abstract Classes

Exemplars are inherited from interface and overridden methods. If you prefer to define method documentation and contracts on interfaces, your Exemplars would be better placed there rather than in the concrete classes. This can be a powerful way to decouple contracts from implementation and is a good way of achieving effective contract-driven-design, and together with Exemplars, test-driven-development.

Classes can have a chain of super-classes and any number of interfaces, so potentially Exemplars for a given method can be inherited from multiple sources. Sureassert UC automatically pulls in and executes inherited Exemplars.

public interface InternationalHello {

    /**
    * Gets a hello message for the given person.
    * @param name The name of the person to say hello to
    * @return The hello message
    */
    @Exemplar(args={"'Joe'"}, expect="retval.contains($arg1)")
    public String hello(String name);

}
public class EnglishHello implements InternationalHello {

    public String hello(String name) {

        return "Hello " + name;
    }
}
public class FrenchHello implements InternationalHello {

    public String hello(String name) {

        return "Bonjour " + name;
    }
}

Running this example gives you the following Sureassert info markers logged against the InternationalHello Exemplar:


- Exemplar evaluation of retval.contains($arg1) returned true (from EnglishHello)

- Exemplar: hello ran successfully and returned 'Hello Joe' (from EnglishHello)

- Exemplar evaluation of retval.contains($arg1) returned true (from FrenchHello)

- Exemplar: hello ran successfully and returned 'Bonjour Joe' (from FrenchHello)

Previous Page   |   Next Page   |   Table of Contents

Comments are closed.