5.5. Default Classes

Default classes are classes generated by Sureassert that implement a given interface using “default methods”.

Please note Sureassert’s Default Class feature is currently in Beta. As of version 1.3.1 Sureassert is not yet capable of generating default implementations for interfaces that define generic types.

A default class implements every method from its interface and returns a default value dependant on the method return type:

Method return type Default class behaviour
void Do nothing
Any numeric primitive (byte, char, short, int, float, double) return 0
boolean (primitive) return false
Any object type return null

Instructing Sureassert to create a default class for an interface is useful when you need a concrete class for your unit test but don’t care about its behaviour, or if you want to use it as a base for stubbing methods and/or verifying interactions.

5.5.1. Default Classes for Method Arguments
The simplest way to use a default class is to use the “default argument” notation in the args propery of an Exemplar on a method that takes an interface as an argument. For example:

@Exemplar(instance="i1", args={"*", "*"})
public void doGet(HttpServletRequest request, HttpServletResponse response)  throws IOException, ServletException {

    HttpSession session = request.getSession();
    // session == null
    ...
}

Here we’ve passed in default implementations of the HttpServletRequest and HttpServletResponse interfaces. Sureassert generates default classes that implement these interfaces, then creates instances of the default class to use in the test. The method calls getSession on the request parameter; this will return null because the default implementation always returns null for object types.

5.5.2. Specifying an Interface for Default Class Generation

It’s also possible to instruct Sureassert to generate a default class for any specified interface. This is achieved with the defaultimpls property:

@Exemplar(instance="i1", args={"*", "*"}, defaultimpls="javax.servlet.http.HttpSession")
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    HttpSession session = request.getSession();
    // session == null
    ...
}

In this example, Sureassert will generate a default class implementing the HttpSession interface. The default class is assigned the name “*InterfaceName” where InterfaceName is the simple (non-qualified) name of the interface the default class was generated from. This named class can then be referenced in other Exemplar properties. See the next section for examples of how to use generated default classes in stubs.

5.5.3. Stubbing Methods on Default Classes

Often you will need your default classes to return a specified value in response to invocations made on objects. The key here is that you can declare stubs for default class methods in the same way you can on any other method in your project.

In the previous example, getSession returned null. This poses a problem if the code-under-test expects it to return a valid HttpSession instance and acts on it, for example:

@Exemplar(instance="i1", args={"*", "*"}, defaultimpls="javax.servlet.http.HttpSession")
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    HttpSession session = request.getSession();
    // session == null

    // set the locale of the user to default if not set
    if (session.getAttribute(WebKeys.LOCALE) == null) {
        session.setAttribute(WebKeys.LOCALE, defaultLocale);
    }

    String fullURL = request.getRequestURI();
    ...
}

The Exemplar above would result in a NullPointerException as the method-under-test invokes getAttribute on the null session instance.

To address this you can define method stubs so that getSession returns a new session, and getAttribute and getRequestURI return test values. The example below illustrates this:

@Exemplar(instance="i1", args={"*", "*"}, defaultimpls="javax.servlet.http.HttpSession", //
    stubs={"*HttpServletRequest.getSession()=new *HttpSession()", //
           "*HttpSession.getAttribute='test'", "*HttpServletRequest.getRequestURI='www.test.com'"})
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                   throws IOException, ServletException {
        HttpSession session = request.getSession();
	// session == null

        // set the locale of the user to default if not set
        if (session.getAttribute(WebKeys.LOCALE) == null) {
            session.setAttribute(WebKeys.LOCALE, defaultLocale);
        }

        String fullURL = request.getRequestURI();

The name “*HttpServletRequest” is assigned by Sureassert to the default class generated by the default argument declaration (args={“*”, “*”}) . The name “*HttpSession” is assigned to the default class generated from the defaultimpls declaration (defaultimpls=”javax.servlet.http.HttpSession”) . These named classes can be used in the same way any other named class is used.

5.5.4. Verifying Methods on Default Classes

You can also verify that default class methods are invoked, or that one is invoked in a particular way. This is achieved using vstubs or the verify property in the same way as is the case for verifying interactions on any other project methods. See the next section for details of how to verify behaviour.

Previous Page   |   Next Page   |   Table of Contents

Comments are closed.