-
Notifications
You must be signed in to change notification settings - Fork 2
About
The test data framework 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.
Some examples:
//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.
If you use the test data framework in you junit test you want to be able to have the same test data when the test fails. To be able to do this the framework can save the seed to generate the test data and then use this seed again when you rerun your test. For TestNg this behaviour will work out of the box, but for junit you will have to add a rule:
@Rule
public TestDataMethodRule rule = new TestDataMethodRule();