In the case of unit tests for data access, which typically add data records, the data set will typically be:
<?xml version= "1.0" encoding= "UTF-8"?>
<dataset>
</dataset>
Then, in the test method, verify that the test method executes correctly by verifying the number of new records or the contents of the new record:
@Test
@DatabaseSetup ("User/userservice-adduser-dataset.xml") public
void Testadduser () {
User user = new User ();
Userservice.adduser (user);
List<user> userlist = (list<user>) userdao.findall ();
Assertequals (1, userlist.size ());
}
The above test method, which works in most cases without problems, leaves a hidden danger: the assertion that validating the number of records is likely to fail if there is a dataset containing user. For example, there is a dataset used in a test:
<?xml version= "1.0" encoding= "UTF-8"?>
<dataset>
<user id= "1" name= "Test One"/>
<user id= "2" name= "Test two"/>
</dataset>
This is the Testadduser method is likely to appear "expect:1,actual:3" failure information.
It is also easy to solve this problem completely by simply setting the table to be blank for the new data:
<?xml version= "1.0" encoding= "UTF-8"?>
<dataset>
<user/>
</dataset>
cause of the problem: Springtestdbunit checks the databasesetup annotation before executing the test, Clean_insert operations on the tables specified in the dataset, and the tables that are not mentioned in the dataset remain unchanged. So to completely empty a table, you have to specify an empty record.