<?xml version="1.0" encoding="ISO-8859-1"?>
<document>
<head>
    <name>Starting Other Tests In A Test</name>
    <doc-version>$Date: 2003/05/04 06:40:13 $</doc-version>
    <author>Matt Albrecht</author>
</head>
<body>
<!--
The code in this document were generated with the jEdit "Code2HTML" plugin.
-->
<P>
With the usage of the <link name="using_iftc">hierarchy tests</link>, 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.
</P>
<P>
Fortunately, I had created the <tt>TestCase</tt> subclass
<tt>SubTestTestCase</tt> which allows for a running test to add additional
tests for execution.  It allowed me to write a test in the following way:
<!--
    A a = (A)createImplObject();
    B b = a.getB()
    InterfaceTestSuite its = BUTestI.suite();
    its.addFactory( new CxFactory( testName ) {
            public Object createImplObject() throws Exception
            {
                return b;
            }
        } );
    addSubTest( its );
-->
<PRE CLASS="javaCode"><SPAN CLASS="gutter">   1:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>A<SPAN CLASS="syntax125"> </SPAN>a<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN>(A)<SPAN CLASS="syntax9">createImplObject</SPAN>();
<SPAN CLASS="gutter">   2:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>B<SPAN CLASS="syntax125"> </SPAN>b<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN>a.<SPAN CLASS="syntax9">getB</SPAN>()
<SPAN CLASS="gutter">   3:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>InterfaceTestSuite<SPAN CLASS="syntax125"> </SPAN>its<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN>BUTestI.<SPAN CLASS="syntax9">suite</SPAN>();
<SPAN CLASS="gutter">   4:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>its.<SPAN CLASS="syntax9">addFactory</SPAN>(<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">new</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">CxFactory</SPAN>(<SPAN CLASS="syntax125"> </SPAN>testName<SPAN CLASS="syntax125"> </SPAN>)<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutterH">   5:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN>Object<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">createImplObject</SPAN>()<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">throws</SPAN><SPAN CLASS="syntax125"> </SPAN>Exception
<SPAN CLASS="gutter">   6:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">   7:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">return</SPAN><SPAN CLASS="syntax125"> </SPAN>b;
<SPAN CLASS="gutter">   8:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutter">   9:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN><SPAN CLASS="syntax125"> </SPAN>);
<SPAN CLASS="gutterH">  10:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">addSubTest</SPAN>(<SPAN CLASS="syntax125"> </SPAN>its<SPAN CLASS="syntax125"> </SPAN>);
<SPAN CLASS="gutter">  11:</SPAN></PRE>
The third line retrieves the <tt>InterfaceTestSuite</tt> 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 <tt>SubTestTestCase</tt>
list of tests to run.
</P>
<P>
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.
</P>
</body>
</document>

