Getting Additional Tests to Run


We make the remaining executable tests run just as we did this first one, except that we've already created the application test class, XTrackTest. We modify each test to add the extends clause and the constructor line, and we add definitions to XTrackTest for the other functions (like login) we use in the remaining tests.

For instance, consider the create and delete user id tests we used as examples in Chapter 18. If we've successfully made LoginStoryTest run as illustrated above, we need to do two things to get these tests running as well. First, we need to add the extends clause and constructor to the UserIdStoryTest, as we did to LoginStoryTest (see Listing 22.2).

Listing 22.2 UserIdStoryTest.java
 public class UserIdStoryTest extends XTrackTest {    public UserIdStoryTest(String name) {super(name);}    public void testCreateDelete() {       login("super","superpassword");       assertTrue( createUserId( "new",                                 "newpassword",                                 "new@xptester.org") );       assertFalse( createUserId("fred",                                 "",                                 "") );       assertTrue( deleteUserId( "new" ) );       assertFalse( deleteUserId("doug") );    } } 

Second, we need to add definitions of createUserId and deleteUserId to XTrackTest. As with login, this requires us (or whoever defines these in XTrackTest) to have knowledge of the code that creates and deletes user ids in XTrack.

To help you understand this example, we'll explain how this code works in XTrack.

XTrackUser is a class in the XTrack system that represents a user identity. It has a create method that defines the id, password, and email address of the user identity and adds it to the database. If the user identity is successfully added to the database, create returns true; otherwise, it returns false. XTrackUser also has a delete method that causes the user identity to be removed from the database (assuming it's in the database). If the delete succeeds, delete returns true; otherwise, it returns false. By default, when a new instance of an XTrackUser is created, it has zero-length values for the id, password, and email address. By supplying an id value to the constructor, you can obtain an XTrackUser representing the user identity in the database with that id.

Consequently, our createUserId needs to create an instance of an XTrackUser and then call its create method, passing the specified user id, password, and e-mail address. It can return whatever create returns. Our deleteUserId needs to obtain an XTrackUser for the id to be deleted and then call its delete method. Listing 22.3 shows the additions required to XTrackTest (marked in bold).

Listing 22.3 XTrackTest.java
 import junit.framework.*; import xtrack.*; public class XTrackTest extends TestCase {    public XTrackTest(String name) {super(name); }    public void assertFalse( boolean b) {          assertTrue(!b);    }        public boolean login( String id,String psw) {         XTrackSession session = new XTrackSession();         return session.login(id,psw);    }    public boolean createUserId( String id, String psw,                                 String email) {          XTrackUser user = new XTrackUser();          return user.create(id,psw,email);    }    public boolean deleteUserId( String id) {             XTrackUser user = new XTrackUser(id);           return user.delete();    } } 


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