net.sourceforge.groboutils.junit.v1.iftc
Class InterfaceTestCase

java.lang.Object
  extended byjunit.framework.Assert
      extended byjunit.framework.TestCase
          extended bynet.sourceforge.groboutils.junit.v1.iftc.InterfaceTestCase
All Implemented Interfaces:
junit.framework.Test

public abstract class InterfaceTestCase
extends junit.framework.TestCase

A subclass of TestCase to ease the requirements of creating an interface test. The tests should be thought of as "contract tests". Subclasses can call createImplObject() to create a new instance of a subclass of interfaceClass, which is generated from the ImplFactory passed into the constructor.

Subclasses that want to use the InterfaceTestSuite helper class will need to specify a constructor similar to:

      public MyClassTest( String name, ImplFactory f )
      {
          super( name, MyClass.class, f );
      }
 
where MyClass is the interface or base class under test.

As of October 30, 2002, the InterfaceTestCase has a slightly different behavior when the factory instance is an implementation of ICxFactory. In this scenario, it will store all the instantiated objects in the stack, and each instantiated object will be passed to the ICxFactory instance during the test's normal tearDown. If the ICxFactory throws an exception during any of the tearDown calls, they will be stored up and reported in a single exception. Therefore, if you want this functionality, then you will need to ensure that your tearDown() method calls the super tearDown().

Even though JUnit 3.8+ allows for a TestCase to have a default (no-arg) constructor, the InterfaceTestCase does not support this. The benefits simply aren't there for interface tests: they will still have to create a constructor which passes InterfaceTestCase which class is being tested. Since a constructor is required anyway, the little extra effort to add two arguments to the constructor and call to the super is trivial compared to not needing the constructor at all.

As of 08-Dec-2002, the returned name of the test can include the class's name, without the package, to improve traceability. This will allow the user to be able to see in which specific test class an error occured through the Ant JUnit report mechanism. This is enabled by default, but can be disabled by setting the Java system-wide property "net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestCase.no-classname" to true, which is dynamically checked at runtime at each call.

Since:
March 2, 2002
Version:
$Date: 2003/02/10 22:52:20 $
Author:
Matt Albrecht groboclown@users.sourceforge.net
See Also:
ImplFactory, ICxFactory, InterfaceTestSuite

Constructor Summary
InterfaceTestCase(java.lang.String name, java.lang.Class interfaceClass, ImplFactory f)
          The standard constructor used by JUnit up to version 3.7.
 
Method Summary
 java.lang.Object createImplObject()
          Calls the stored factory to create an implemented object.
 java.lang.Class getInterfaceClass()
          Return the interface or abstract class this test covers.
 java.lang.String getName()
          Override the TestCase default getName so that the factory names are returned as well.
 java.lang.String name()
          Ensure, for JUnit 3.7 support, that the original name() method is still supported.
 void setUseClassInName(boolean use)
          Sets whether the classname is put in the output or not.
protected  void tearDown()
          Send each instantiated object to the factory for cleanup.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, run, run, runBare, runTest, setName, setUp, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

InterfaceTestCase

public InterfaceTestCase(java.lang.String name,
                         java.lang.Class interfaceClass,
                         ImplFactory f)
The standard constructor used by JUnit up to version 3.7.

Parameters:
name - the name of the test to execute.
interfaceClass - the class which this test case tests.
f - the factory which will create specific subclass instances.
Method Detail

setUseClassInName

public void setUseClassInName(boolean use)
Sets whether the classname is put in the output or not. If you don't set this value here, it will use the value of the system property described above.

Since:
03-Dec-2002

createImplObject

public java.lang.Object createImplObject()
Calls the stored factory to create an implemented object. Subclasses should make their own method, say getObject(), which returns this method's result, but casted to the right class.

This method makes an assertion that the factory's created object is not null, so that the system state is ensured. Therefore, this method will never return null. Also, this method asserts that the created object is of the correct type (as passed in through the constructor), so that it can be correctly cast without errors.

Returns:
the object created by the factory.

getInterfaceClass

public java.lang.Class getInterfaceClass()
Return the interface or abstract class this test covers.

Returns:
the interface under test.

getName

public java.lang.String getName()
Override the TestCase default getName so that the factory names are returned as well.

Returns:
the method name being tested, along with the factory's name.

name

public java.lang.String name()
Ensure, for JUnit 3.7 support, that the original name() method is still supported.

Returns:
getName().

tearDown

protected void tearDown()
                 throws java.lang.Exception
Send each instantiated object to the factory for cleanup.

Throws:
java.lang.Exception - thrown if the super's tearDown throws an exception, or if any exceptions are thrown during the tear-down of the factory generated instances.


Copyright © 2001-2003 by The GroboUtils Project