Table of Contents
The purpose of this tool is to capture test data for unit testing in Java applications. It does this by intercepting method calls in the application and logging the contents of objects that you want to use for unit testing. The format of the logged data is in a format that can be readily used as test input for tests in JUnit or TestNG, for example.
// Generated 2011-03-14 17:49:42,390 // org.springframework.samples.jpetstore.domain.Order.initOrder:Parameter1 public org.springframework.samples.jpetstore.domain.Account createParam1Account_org_springframework_samples_jpetstore_domain_Order_initOrder() { org.springframework.samples.jpetstore.domain.Account account0 = new org.springframework.samples.jpetstore.domain.Account(); account0.setUsername("j2ee"); account0.setPassword(null); account0.setEmail("yourname@yourdomain.com"); account0.setFirstName("ABC"); account0.setLastName("XYX"); account0.setStatus("OK"); account0.setAddress1("901 San Antonio Road"); account0.setAddress2("MS UCUP02-206"); account0.setCity("Palo Alto"); account0.setState("CA"); account0.setZip("94303"); account0.setCountry("USA"); account0.setPhone("555-555-5555"); account0.setFavouriteCategoryId("DOGS"); account0.setLanguagePreference("english"); account0.setListOption(true); account0.setBannerOption(true); account0.setBannerName("<image src=\"../images/banner_dogs.gif\">"); return account0; }
The generated logging could then be pasted into a Java class, and the generated methods invoked to recreate the object containing the data you want to test. That test object could then be used in unit tests for JUnit, TestNG, etc.
Unlike other test data generation utilities, the primary purpose of this tool is:
to be used in a Java development environment
to capture complex non-persistent data
to capture 'real' data for unit testing as opposed to mock data you make up yourself
Note that this tool is meant to used to recreate test objects that are basically data holders and follow the JavaBean naming conventions for field access. Also you need to have some basic knowledge of AspectJ, especially regarding pointcuts, to use it.
The TestDataCaptureJ project is available for download as an Eclipse project at Github. Please see the installation instructions for the requirements.
The original purpose for creating this tool was to capture data to be used in unit testing from an enterprise web application that I was working on. The data I was after was the contents of a shopping basket in the checkout process.
Some of ways I could have tried to do this were:
to create the data manually as mock objects. However this would have required too much work because:
The basket items were not simple items but were more complex combinations of items. Some objects contained object graphs which included some fairly large objects (about 40 fields in one class).
I needed many different items in various combinations for many test scenarios.
to create the data from the database. However this would have not have worked because:
The basket items were not persistent and did not exist in the database in that form, that is they only existed in the user web session.
A shared development database was being used by the developers on the project, and the data would be changed at regular intervals with a database refresh from the production system. That meant that selenium tests would go out of date because items that were used in the tests would no longer be available after the database refresh.
Using the TestDataCaptureJ tool, I was able to capture the data used in the selenium tests and use them in JUnit tests that could be re-run without depending on the state of the development database.
Instructions on installation and setup
Or you can skip ahead to other topics: