About GroboUtils


For Developers

GroboTestingJUnit version 1.2.1

Starting Other Tests In A Test

Author:Matt Albrecht

With the usage of the hierarchy tests, I have encountered situations where my unit test for a method in interface A returns an instance of interface B. I thought that my existing tests for interface B would be perfect for adding extra insurance on the returned instance.

Fortunately, I had created the TestCase subclass SubTestTestCase which allows for a running test to add additional tests for execution. It allowed me to write a test in the following way:

   1:    A a = (A)createImplObject();
   2:    B b = a.getB()
   3:    InterfaceTestSuite its = BUTestI.suite();
   4:    its.addFactory( new CxFactory( testName ) {
   5:            public Object createImplObject() throws Exception
   6:            {
   7:                return b;
   8:            }
   9:        } );
  10:    addSubTest( its );
The third line retrieves the InterfaceTestSuite instance from the interface test case for interface B. The following line adds a new factory to the interface test case that returns the newly discovered B instance. Finally, the test suite is added to the SubTestTestCase list of tests to run.

The above method isn't perfect: if the tests modify the instance in any way, then all executed tests will be modifying the same instance, making error tracing difficult at best. However, for many situations, this helps in assuring that a returned object conforms to all required standards.

SourceForge Logo
This space graciously provided by the SourceForge project
Copyright © 2002-2004 GroboUtils Project.
All rights reserved.