Browse DevX
Sign up for e-mail newsletters from DevX


jDefend: One Configuration File for All Your Test Scenarios  : Page 2

How would you like to have a test driver that you don't need to recompile every time input parameters change?




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Test the StudentManager Class with jDefend
To test the functionality of the StudentManager class, you ordinarily would take one of the following three approaches:

  • Hard code the test data inside your test driver. That is, create the Student object by hard coding all the data fields.
  • Write a test driver class that accepts command line parameters.
  • Read the data from a file.

    Not only do all these approaches require a good bit of coding, they also are inflexible. If you added a new attribute to the Student class, you'd have to re-code and re-compile the test driver to accommodate the new change.

    To see how jDefend offers a better solution for this situation, add a new attribute called Address (see Listing 3: Address.java below) to the Student class. Address is another java class, which has the address details of the Student object.

    Listing 3: Address.java
    package example; public class Address { String street; String state; String city; String zip; public void setStreet(String val) { street = val; } public void setState(String val) { state = val; } public void setCity(String val) { city = val; } public void setZip(String val) { zip = val; } }

    With jDefend, you need to change only the XML-formatted configuration file to accommodate this new class. You set all your input values, method participation attributes, etc. in this one file.

    Implement a test driver class called StdMgrTestDriver to test the StudentManager class. To write your test driver class, just extend it from the jDefend.TestDriver.

    package example; import testSuite.*; public class StdMgrTestDriver extends TestDriver { StudentManager mgr = new StudentManager(); public boolean testAdd(Student s) { mgr.add(s); return true; } public boolean testRemove(Student s) { mgr.remove(s); return true; } public boolean testFindStudent(String name) { Student std = mgr.findStudent(name); if (std == null) { return false; } return true; } }

  • Comment and Contribute






    (Maximum characters: 1200). You have 1200 characters left.



    Thanks for your registration, follow us on our social networks to keep up-to-date