Refactoring the Application Test Class


We know this is sounding complicated just hang on and you'll see where we're going. The next step is to refactor XTrackTest by removing the code we moved to XTrackDirectInterface and replacing it with code that can use either the direct interface or the WebART interface (creating the WebART interface will be the last thing we do). Listing 23.3 shows what the new version of XTrackTest looks like after we've done this.

Listing 23.3 XTrackTest.java
 import junit.framework.*; public class XTrackTest extends TestCase {    public XTrackTest(String name) {          super(name);          setTestInterface("direct");    }    public void assertFalse( boolean b) {          assertTrue(!b);    }    private XTrackTestInterface testInf;    public void setTestInterface(String interfaceType){             if (interfaceType.equals("direct"))             testInf = new XTrackDirectInterface();             else if (interfaceType.equals("webart"))                testInf = new XTrackWebARTInterface();             else                fail("Undefined interface " +                 interfaceType);    }    public boolean login( String id,String psw) {           return testInf.login(id,psw);    }    public boolean createUserId( String id,                                 String psw,                                 String email) {              return testInf.createUserId(id,psw,email);    }    public boolean deleteUserId( String id) {           return testInf.deleteUserId(id);    } } 

We've removed the import xtrack.*; statement, because we no longer need it. That's because we no longer have a direct connection between XTrackTest and the XTrack system. That direct connection is now encapsulated in XTrackDirectInterface (which now needs the import statement, as we pointed out above).

We've modified the XTrackTest constructor so it's no longer simply a pass-through to super (the constructor in TestCase). It still calls super, but after that it also calls the setTestInterface method to set its interface to direct. This means that by default, this version of XTrackTest will use the direct-call interface, the one we already have working.

We made no changes to assertFalse. The next new thing is the declaration of an XTrackTestInterface named testInf. Understanding how this is used is bound up in how the setTestInterface method works.

The setTestInterface method checks if direct was specified. If so, it creates a new XTrackDirectInterface and stores it in testInf. If webart was specified, it creates an XTrackWebARTInterface and stores that in testInf. In other words, this method creates an object for the appropriate type of interface (either for direct access or access via WebART) and stores that object in testInf.

Having created an object for the appropriate interface in testInf, our new version of XTrackTest simply needs to pass the parameters to corresponding methods of testInf in its login, createUserId, and deleteUserId methods and return the same values testInf's methods return.



Testing Extreme Programming
Testing Extreme Programming
ISBN: 0321113551
EAN: 2147483647
Year: 2005
Pages: 238

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net