• Home
  • About
  • Testdata generation

    We’ve all being there, we’ve all had this on a project once or maybe even more times. The assignment is to build an application, but there is no data for you to work with. There could be any number of reasons this could be the case to name a few, the web-service that should be connected is not done in time, the database migration is postponed. Then someone has to create database scripts with test data, or implement a test web-services. This is all a waste of time.

    But lucky for you now there is a solution.

    I’ve created an little test data framework that is easy to setup and will create test data automatically. So how does it work? Simple, its entry point is a method interceptor that you can wrap around anything you want, it doesn’t even have to be an implementation. So that DAO you want to use, but the database is still empty, just let the framework step into place and all the methods return the data that you want.

    It can also be handy to create test data for you unit test. You need to test if you bean serialization works, or want to put an entity into a in memory database with dbunit. Just call the framework with you bean class and the framework will return an instance filled with test data. It will read the existing annotations that you have on your entity bean an will only generate data that is valid for you bean.

    Here is an example of how it works:

    
     //Create an instance of the Employee class and fill it with test data.
     Employee employee = TestData.createBeanInstance(Employee.class);
    
     //Annotations that restrict the data for first name are recognized.
     assertNotNull(employee.getFirstName());
    

    To create a service that returns test data on it’s service methods:

    
     //create a instance of the service class/interface that will return test data
     Service exampleService = TestData.createService(Service.class);
    
     //this now returns a list of employee instances that are filled with test data
     List<Employee> employees = exampleService.findByName("name");
    

    Create a dbunit xml file:

    
     //create a file
     File fileLocation = File.createTempFile("file", ".xml");
    
     //populate the file with test data for the employee
     TestData.createDBUnitDataSet(Employee.class, fileLocation);
    

    If employee has relations with other classes, like employees have managers these objects will also be in the generated xml file.

    To give it a spin add the following into you pom.xml

    
         <dependency>
             <groupId>ch.nerdin</groupId>
             <artifactId>testdata-framework</artifactId>
             <version>0.10</version>
         </dependency>
    

    or if you use ivy, gradle or something else you properly know how to use this information as well.

    So nothing holding you back to make your data driven test easier to setup, or to stub out you migration points. I’m hosting this code on github so if you have issues, comments or even better contribute please do so.

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • Y!GG
    • Webnews
    • Digg
    • del.icio.us
    • DotNetKicks
    • Facebook
    • Google Bookmarks
    • Newsrider
    • Newstube
    • TwitThis
    • YahooBuzz

    8 Comments

    1. Marcin said,

      September 27, 2011 @ 7:19 am

      Now this is a handy piece of code!

    2. Erik Jan said,

      September 27, 2011 @ 8:47 am

      Give it a spin and tell me what you think!

    3. Alleiah said,

      September 28, 2011 @ 8:16 am

      Ealrier i have tried to generate the testing tool which generates the Unit testcases.
      Nice to see that Testdata Generation and i am eager to try this framework once.

    4. Mol said,

      September 28, 2011 @ 9:32 am

      I was looking for such a tool for some time. thanks

    5. Moncler men said,

      October 2, 2011 @ 12:12 am

      I really could spend hrs looking through you, continue the good work!

    6. Leeann Plotz said,

      October 3, 2011 @ 4:00 pm

      I’m not sure where you’re getting your information, but great topic. I needs to spend some time learning more or understanding more. Thanks for excellent information I was looking for this info for my mission.

    7. Ashwin Jayaprakash said,

      October 4, 2011 @ 11:06 pm

      Are you aware of the other tools like – http://databene.org/benerator and http://www.webresourcesdepot.com/test-sample-data-generators/?

    8. Erik Jan said,

      October 5, 2011 @ 8:02 am

      No, I wasn’t aware of these products. But seems that these products that you mention are a set of tools to generate test data for databases and xml, with my testdata-framework one can integrate easily with existing code. As there is no need to setup a database. It will generate data based on you Entities or Service beans.

    RSS feed for comments on this post