Defining the Application Test Class


Next, we need to define XTrackTest. We're using this name because these are tests for the XTrack application. If it were the Directory application, we'd name it DirectoryTest, and so on. Our definition of XTrackTest needs to provide, directly or indirectly, the assertTrue, assertFalse, and login functions (see Listing 22.1).

Listing 22.1 XTrackTest.java
 import junit.framework.*; import xtrack.*; public class XTrackTest extends TestCase {    public XTrackTest(String name) {super(name); }    public void assertFalse( boolean condition) {          assertTrue(!condition);    }    public boolean login( String id, String psw) {         XTrackSession session = new XTrackSession();         return session.login(id,psw);    } } 

The first line in this example, import junit.framework.*; , tells the Java compiler where to find the code for the JUnit framework. The JUnit framework contains assertTrue, assertEquals, and many other useful functions.

The second line, import xtrack.*; , tells the Java compiler where to find the code for the XTrack system. This is where the login code we want to test resides. More about this a bit later.

On the third nonblank line, we define XTrackTest and specify that it, in turn, does not stand alone but is a variant of something called a TestCase. This means that whatever methods a TestCase has, an XTrackTest has the same ones, plus whatever additional ones we define here. TestCase is defined in the JUnit framework, so we don't need to define it. Even better, it endows our XTrackTest with the assertTrue and assertEquals methods (among others).

The next line contains a constructor, just like the one in LoginStoryTest (except for its name). When LoginStoryTest calls super in its constructor, it actually calls this code. This constructor in turn calls super, which is the constructor of TestCase, where the work of creating an instance is finally carried out.

The next three nonblank lines are a definition of assertFalse. At the time of this writing, JUnit does not define an assertFalse method, so we have to define our own. Given assertTrue, however, it's pretty easy to build an assertFalse, as you can see.



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