Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Web Development

Change Code Without Fear


Extending the Test Suite

Before extending the test suite, it's helpful to review the automatically generated test cases. For example, Listing One is a sample test that's automatically generated for the com.ibatis.jpetstore.domain.Order class (specifically, its initOrder() method). Notice how the tool automatically generated input data to use for the test. At the same time, it created assertions based on the observed state of the objects at the end of the test. This now becomes a test that you can always rerun against the Order class to confirm that its behavior remains intact as you are extending and modifying it.

Further, you can extend the automatically generated test suite either by manually adding more test cases or by modifying the automatically generated tests to use realistic data, check specific assertions, or test various functional scenarios that you may be interested in. Here is an example of a test for the initOrder() method in the com.ibatis.jpetstore.domain.Order class. Listing Two (available online at http://www.ddj.com/code/) is a test created by manually extending one of the automatically generated tests to add more logical, realistic data—as well as to add more assertions.

/**
* Test for method: initOrder(com.ibatis.jpetstore.domain.Account,com.ibatis.jpetstore.domain.Cart)
*/
public void testInitOrder21() throws Throwable {
   Order testedObject = new Order();
   Account account = new Account();
   Cart cart = new Cart();
   account.setUsername("username1");
   account.setPassword("password0");
   account.setEmail("email0");
   account.setFirstName("firstName0");
   account.setLastName("lastName0");
   account.setStatus("status1");
   account.setAddress1("140 East 45th Street, New York, NY 10017");
   account.setAddress2("1600 Pennsylvania Avenue NW, Washington, DC 20500");
   account.setCity("Tokyo");
   account.setState("New York");
   account.setZip("90011-1234");
   account.setCountry("England");
   account.setPhone("1234567");
   account.setFavouriteCategoryId("favouriteCategoryId0");
   account.setLanguagePreference("languagePreference0");
   account.setListOption(false);
   account.setBannerOption(false);
   account.setBannerName("bannerName0");
   testedObject.initOrder(account, cart);
   assertNotNull(testedObject.getLineItems());
   assertEquals(0, testedObject.getLineItems().size());
   assertNotNull(testedObject.getOrderDate());
   assertEquals(new java.util.Date().toString(), testedObject.getOrderDate().toString());
   assertNotNull(testedObject.getTotalPrice());
   assertEquals("0", testedObject.getTotalPrice().toString()); 
   assertEquals("P", testedObject.getStatus());
   assertEquals(0, testedObject.getOrderId());
   assertEquals("username1", testedObject.getUsername());
   assertEquals("140 East 45th Street, New York, NY 10017", testedObject.getShipAddress1());
   assertEquals("1600 Pennsylvania Avenue NW, Washington, DC 20500",testedObject.getShipAddress2());
   assertNotNull(cart.getAllCartItems());
   assertNotNull(cart.getCartItems());
}
Listing One

Over time, you might want to further improve the intelligence of the regression test suite by using automated tools that record interactions with a running application, then produce functional JUnit tests representing the recorded interactions. Listing Three (available online) is an example of a front-end JUnit test that represents a scenario of purchasing a dog and a cat, then validates that the total for this order is $112.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.