Package net.sourceforge.groboutils.junit.v1.iftc

Classes to aid in testing interfaces, abstract classes, and base classes for "contract tests".

See:
          Description

Interface Summary
ICxFactory An ICxFactory is an extension of ImplFactory that has provisions for cleaning up generated objects on test tear down.
ImplFactory Allows for tests to be written on interfaces or abstract classes, by creating a specific instance of the interface or abstract class.
 

Class Summary
CxFactory Helper abstract class that aids in the setting of a unique and distinguishable name for a test case's factory.
InterfaceTestCase A subclass of TestCase to ease the requirements of creating an interface test.
InterfaceTestSuite Allows for tests to be written on interfaces or abstract classes.
 

Package net.sourceforge.groboutils.junit.v1.iftc Description

Classes to aid in testing interfaces, abstract classes, and base classes for "contract tests".

Interface Testing

The Sun Java Tutorial indicates that classes which implement an interface "[sign] a contract" [1]. In some cases the contract has meta-data beyond the API declared in the interface source-code, such as "must never return null", usually declared in the JavaDoc. Forcing each implemented class to test this on its own is both poor coding practice and unenforcable. Instead, what we need is a way to write test cases for the interface, and each implemented class can execute an instance of that class against the tests. This framework eases this practice by extending the JUnit framework.

The interface creator creates a TestCase which extends InterfaceTest, commonly through InterfaceTestCase. The class to test need not be only an interface: in can be any kind of class.

Implemented classes need to use something like the following to test through an interface test:

import net.sourceforge.groboutils.junit.v1.iftc.*;
import junit.framework.*;

public MyClassTest extends TestCase {
    public MyClassTest( String name ) {
        super( name );
    }

    public static Test suite() {
        TestSuite suite = new TestSuite( MyClassTest.class )
        InterfaceTestSuite its = MyInterfaceTest.suite();
        its.addFactory( new CxFactory( "A" ) {
            public Object createImplObject() {
                return new MyClass( "string" );
            }
        } );
        its.addFactory( new CxFactory( "B" ) {
            public Object createImplObject() {
                return new MyClass( null );
            }
        } );
        suite.addTest( its );
        
        return suite;
    }
    ...
}
The interface test would then look something like:
import net.sourceforge.groboutils.junit.v1.iftc.*;
import junit.framework.*;

public MyInterfaceTest extends InterfaceTestCase {
    public MyInterfaceTest( String name, ImplFactory f ) {
        super( name, MyInterface.class, f );
    }

    public static InterfaceTestSuite suite() {
        InterfaceTestSuite suite = new InterfaceTestSuite(
            MyInterfaceTest.class );
        
        return suite;
    }
    
    ...
}

References

  1. Various. The Java Tutorial. Online at http://java.sun.com/docs/books/tutorial/java/interpack/usinginterface.html .



Copyright © 2001-2003 by The GroboUtils Project