问题
I have a problem with a db unit test which tests if data is persisted correctly. Therefore I created an sample db with my data and tried to compare the setup and the expected data. The id generation and everything else should be managed by hibernate
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DatabaseTearDown("empty.xml")
public class UserDaoTests {
@Autowired
UserAdminDao userDao;
@Test
@DatabaseSetup("db-setup.xml")
@ExpectedDatabase("db-expected.xml")
public void testPersistUser(){
User user = new User();
user.setUserId("user2");
user.setName("test2");
user.setEmail("user2@email.com");
user.setLocked(false);
user.setEnabled(true);
user.setVersion(0);
user.setPassword("asdfasdf");
userDao.persist(user);
}
}
And my setups are:
<dataset>
<user id="1" userId="user1" name="test1" email="user1@email.com" locked="0" enabled="1" version="0" password="asdfasdf" />
</dataset>
and the same with user id=2 underneath:
<user id="2" userId="user2" name="test2" email="user2@email.com" locked="0" enabled="1" version="0" password="asdfasdf" />
But I'm getting an Comparison error:
junit.framework.ComparisonFailure: row count (table=user) expected:<[2]> but was:<[1]>
Not sure where my mistake is. thx for any help :)
回答1:
You have to add table name to "@ExpectedDatabase" Something like this @ExpectedDatabase(value = "dataset.xml", table = "your_table_name")
来源:https://stackoverflow.com/questions/21909399/dbunit-test-comparison-failure