Search code examples
javaunit-testingjibx

Creating xml test data in java from Oracle database


When we encounter production issues, it becomes difficult to debug them, firstly due to no access to production database and secondly since the component is so heavy, it doesn't come up on local machines. All we are left with is manually traverse code line by line and guess where it may have gone wrong.

Less preferred way is creating a TestNg unit test which resembles the production scenario and debug. Less preferred because data is huge, lots of tables and objects involved so creating the data is too time consuming.

In the unit tests, test data is created in xmls, which mirrors the orm object attributes. For e.g Employee : id,name,phoneNumber

<Employee>
   <id>1<id>
   <name>john</name>
   <phoneNumber>12345</phoneNumber>
</Employee>

On test startup, these are unmarshalled to data objects through Jibx and persisted into in-memory H2 database, which can then be used as a sample data for test cases.

I am looking for a way to automate this test xml data creation. Something which will convert the test data from a uat database and create these xmls. This will help us debug the issues right away, as long as we have similar data in our development database.


Solution

  • When I have encountered this in the past, I have used short Java programs to read the records from the database and generate the xml file. If you are using JDBC (Java Data Base Connectivity), the ResultSet and ResultSetMetaData interfaces (https://docs.oracle.com/javase/8/docs/api/java/sql/package-summary.html) can enable you to make fairly flexible tools.