What is a good strategy for approving updated data in a database using DBUnit?

Here is an example of (overly) simplified code to describe the unit testing method.

CompanyDataSet.xml

<dataset> <company company_key="100" company_name="OldName" /> </dataset> 

CompanyDaoTest.java

 @Test public void testUpdateCompany() { CompanyDao companyDao = new CompanyDao(); IDatabaseConnection dbConn = createConnection(); IDataSet dataSet = createDataSet("CompanyDataSet.xml"); DatabaseOperation.CLEAN_INSERT.execute(dbConn, dataSet); companyDao.updateCompany(100, "NewName"); // What is a good way to assert updated company data ? } 

I came up with two ways to approve company data.

Create another xml dataset as the expected dataset.

different xml

 <dataset> <company company_key="100" company_name="NewName" /> </dataset> 

Approve Part in Java

 IDataSet actual = dbConn.createDataSet(new String[]{"company"}); IDataSet expected = createDataSet("CompanyDataSet_expected.xml"); Assertion.assertEquals(expected, actual); 

Just upload the company object through the DAO and compare the properties.

you should get this idea.


My problem

The first method is quite easy to write, but I need to create a different XML file for each other update method. The dose does not seem to be a good idea for creating so much XML data files.

The second method is simple, however, when there are various update methods, the test class is populated with methods that claim different properties with different values. And Lot's tests will break if something is wrong with the boot method.

Is there a good way to validate the data? Is it possible to avoid the problem that I just described (or is it really not important)?

UPDATE

Since no one answers the question, I decide to accept my answer.

+6
source share
2 answers

Here is another way to confirm the data.

Since I already have the actual IDataSet that contains the current database data. I can just extract data from it.

I use this example, this is what I do

 @Test public void testUpdateCompany() { CompanyDao companyDao = new CompanyDao(); IDatabaseConnection dbConn = createConnection(); IDataSet dataSet = createDataSet("CompanyDataSet.xml"); DatabaseOperation.CLEAN_INSERT.execute(dbConn, dataSet); companyDao.updateCompany(100, "NewName"); IDataSet actual = dbConn.createDataSet(new String[]{"company"}); ITable companyTable = actual.getTable("company"); assertEquals("NewName", companyTable.getValue(0, "company_name")); } 

This is somewhat verbose, if there are many properties to check, but it should be fairly easy to handle


UPDATE

Although this method solves the problem, the test will no longer bind to an unrelated method. But, when it comes to service, he actually has the same overhead, in the first place, even worse. Because I need to write the column name (many of them) inside java code, and there is no way to verify this.

Our team decides to use the data set to validate the data whenever possible. The main reason is that we want our test data to be as small as possible and easily validated.

So the answer to the question will be using a dataset

However, there will be some drawbacks, here's how we handle it:

  • Organize the XML files:

    Location: Dataset XML files will be in one package of test classes.

    Naming: the XML file will start with the name of the test class and add the method / state name if necessary. for example: "companyDao-updateCompany-expected.xml"

  • Make XML synchronization with the database.

    We will use Untils to generate a DTD file from the database on the fly and use it to validate XML. We rarely change the name of a column, so the overhead should be low.

+1
source

I tried to test databases for a long time, and I gathered my thoughts on several blogs:

+4
source

Source: https://habr.com/ru/post/915773/


All Articles