net.sourceforge.groboutils.junit.v1.iftc |
net.sourceforge.groboutils.testing.junit.v1
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
- Various. The Java Tutorial. Online at
http://java.sun.com/docs/books/tutorial/java/interpack/usinginterface.html .
|
Java Source File Name | Type | Comment |
AntJUnitEUTest.java | Class | Tests the functionality of the Ant JUnit optional tasks for what is
expected in operation of the naming facilities. |
CxFactory.java | Class | Helper abstract class that aids in the setting of a unique and
distinguishable name for a test case's factory.
As of 08-Dec-2002, the original constructor will NOT add the owning
class's name to the factory toString() output. |
CxFactorySample.java | Class | Sample CxFactory used in CxFactoryUTest. |
CxFactoryUTest.java | Class | Tests the CxFactory class. |
ICxFactory.java | Interface | An ICxFactory is an extension of ImplFactory that has provisions for
cleaning up generated objects on test tear down. |
ICxFactoryUTestI.java | Class | Tests the ICxFactory interface. |
ImplFactory.java | Interface | Allows for tests to be written on interfaces or abstract classes, by creating
a specific instance of the interface or abstract class. |
ImplFactoryUTestI.java | Class | Tests the ImplFactory interface. |
InnerClassNameEUTest.java | Class | Tests the functionality of the JUnit TestSuite class for conformance to
expected behaviors. |
InterfaceTestCase.java | Class | A subclass of TestCase to ease the requirements of creating an
interface test. |
InterfaceTestCaseUTest.java | Class | Tests the InterfaceTestCase class. |
InterfaceTestSuite.java | Class | Allows for tests to be written on interfaces or abstract classes. |
InterfaceTestSuiteUTest.java | Class | Tests the InterfaceTestSuite class. |
Sample1.java | Interface | Sample interface. |
Sample1IUTestI.java | Class | Tests the Sample1 interface. |
Sample2.java | Interface | Sample interface. |
Sample2IUTestI.java | Class | Tests the Sample2 interface. |
Sample3.java | Class | Sample abstract class. |
Sample3Impl.java | Class | Sample implementation. |
Sample3ImplIUTest.java | Class | Tests the Sample3Impl class. |
Sample3IUTestI.java | Class | Tests the Sample3 abstract class. |
Sample4.java | Interface | Sample interface. |
Sample4IUTestI.java | Class | Tests the Sample4 interface. |