Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Java Tools for Extreme programming mastering open source tools including - Hightower R.

Hightower R. Java Tools for Extreme programming mastering open source tools including - Wiley publishing , 2002. - 417 p.
ISBN: 0-471-20708
Download (direct link): javatoolsforextremeprog2002.pdf
Previous << 1 .. 54 55 56 57 58 59 < 60 > 61 62 63 64 65 66 .. 159 >> Next

package test.xptoolkit.model;
172
import java.util.*; import junit.framework.*;
import xptoolkit.petstore.model.Category; import xptoolkit.petstore.model.Product; import xptoolkit.petstore.model.Subcategory;
public class CategoryTest extends TestCase {
Category category; //object under test
public CategoryTest(java.lang.String testName) { super(testName);
}
public static void main(java.lang.String[] args) { junit.textui.TestRunner.run(suite());
}
public static Test suite() {
TestSuite suite = new TestSuite(CategoryTest.class);
return suite;
}
public void setUp()throws Exception{ category = Category.getCategory(); category.setId(777);
}
173
/** Test of getCategory method, of class xptoolkit.petstore.model.Category. */ public void testGetCategory() throws Exception{ System.out.println("testGetCategory");
Category category = Category.getCategory(); category.setId(777);
this.assertNotNull("category", category);
}
/** Test of getSubcategories method, of class xptoolkit.petstore.model.Category. */ public void testGetSubcategories() throws Exception { Subcategory [] categories = category.getSubcategories(); assertNotNull("categories", categories); for (int index=0; index < categories.length; index++){ assertNotNull("subcategory", categories[index]);
}
}
/** Test of getSubcategory method, of class xptoolkit.petstore.model.Category. */ public void testGetSubcategory() throws Exception { Subcategory [] categories = category.getSubcategories(); assertNotNull("categories", categories); for (int index=0; index < categories.length; index++){ Subcategory subcat=categories[index]; int id = subcat.getId();
assertNotNull("subcategory", category.getSubcategory(id));
}
}
174
public void testGetters() throws Exception { assertNotNull(''name'', category.getName()); assertNotNull(''description'', category.getDescription());
}
}
There are four tests to test the public interface of xptoolkit.petstore.model.Category, as follows:
? testGetCategory
? testGetSubcategories
? testGetSubcategory
? testGetters
The setUp() method creates a category instance and sets it to the main test category. category - Category.getCategory(); category.setId(777);
Remember, setUp() is called for each test. Thus, each test gets its own copy of category. For example, testGetSubcategories gets its own copy of category, which it uses to test the getSubcategories() method of xptoolkit.petstore. model.Category as follows:
public void testGetSubcategories() throws Exception {
Subcategory [] categories - category.getSubcategories();
assertNotNull(''categories'', categories);
for (int index-0; index < categories.length; index++){
assertNotNull(''subcategory'', categories[index]);
}
}
Because the test data is known, we could check for specific values of subcategories in specific locations. For example, look at testGetters() from the ProductTest class:
/** Test of getters method of class
xptoolkit.petstore.model.Product. */
public void testGetters() {
this.assertEquals(''name'', product.getName(), ''Poodle'');
this.assertEquals(''description'', product.getDescription(),
'Poodle description');
testSetters();
this.assertEquals(''name'', product.getName(), ''Boo''); this.assertEquals(''description'', product.getDescription(),
'Designer');
}
Note that each main model class in the model project has a corresponding test class in the test project (see Figure 7.2).
175
9 1=3 C?:VS^PToo*<^aseStLJdtes^nlineEntr?_JUfHae?tVwa l3 C.^CVSy(PTooKlliCaseStuciie?>DntineErlrv_JLMll*KteH)iava
Figure 7.2: Parallel Class Structure Test and Model.
The test buildfile is responsible for executing the tests in an automated fashion (Listing 7.5). Typically, you write the tests as you develop your classes. Once you are done writing your tests and using them to incrementally test your code as you write it, you put them in your test buildfile so they are included in your automatic build and deploy. Then, not only can you break the build by checking in code that does not compile, you can break the build by writing code that breaks the tests.
Listing 7.5: Test Project Buildfile.
<project name="test" default="all" >
<target name="setProps" unless="setProps"
description="setup the properties.">
<property name="outdir" value="/tmp/petstore" />
</target>
<target name="init" depends="setProps"
description="initialize the properties.">
<tstamp/>
<property name="local_outdir" value="${outdir}/pettest" /> <property name="lib" value="${outdir}/lib" />
<property name="dist" value="${outdir}/dist" />
176
<property name="reports" value="${outdir}/reports" /> <property name="build" value="${local_outdir}/testclasses" /> <property name="build_lib" value="./../lib" />
<property name="test_lib" value="./../testlib" />
<property name="test_jar" value="${lib}/pettest.jar^' /> </target>
<target name="clean" depends="init"
description="clean up the output directories."> <delete dir="${local_outdir}" />
<delete dir="${reports}" />
</target>
Previous << 1 .. 54 55 56 57 58 59 < 60 > 61 62 63 64 65 66 .. 159 >> Next