1、在pom.xml文件中加入依赖:
<!-- Spring+Dbunit -->
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/beans.xml")
@TestExecutionListeners({DbUnitTestExecutionListener.class,DependencyInjectionTestExecutionListener.class}) //1、使用dbunit
public class TestUserDao {
@Inject
private IUserDao userDao;
@Test
@DatabaseSetup("/t_user.xml")//2、使用dbunit 3、导包
public void testLoad() throws DatabaseUnitException, SQLException{
User u = userDao.load(1);
EntitiesHelper.assertUser(u);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<t_user id="1" username="admin1"/>
<t_user id="2" username="admin2"/>
<t_user id="3" username="admin3"/>
<t_user id="4" username="admin4"/>
<t_user id="5" username="admin5"/>
<t_user id="6" username="admin6"/>
<t_user id="7" username="admin7"/>
<t_user id="8" username="admin8"/>
<t_user id="9" username="admin9"/>
<t_user id="10" username="admin10"/>
<t_user id="11" username="admin11"/>
<t_user id="12" username="admin12"/>
<t_user id="13" username="admin13"/>
<t_user id="14" username="admin14"/>
<t_user id="15" username="admin15"/>
<t_user id="16" username="admin16"/>
<t_user id="17" username="admin17"/>
<t_user id="18" username="admin18"/>
</dataset>