??Spring?н??Dbunit??Dao???м?????????
???????????? ???????[ 2013/12/23 10:37:14 ] ????????
package com.test.dbunit.dao;
import javax.sql.DataSource;
import org.dbunit.Assertion;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.QueryDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import com.test.dbunit.entity.User;
@ContextConfiguration(locations = { "classpath:testApplicationContext.xml" })
@TransactionConfiguration(defaultRollback = true)
public class UserDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private UserDao userDao;
@Autowired
private DataSource dataSource;
private IDatabaseConnection conn;
@Before
public void initDbunit() throws Exception {
conn = new DatabaseConnection(DataSourceUtils.getConnection(dataSource));
}
@Test
public void saveUser() throws Exception {
User user = new User();
user.setNick("user001");
user.setPassword("password001");
userDao.save(user);
QueryDataSet actual = new QueryDataSet(conn);
actual.addTable("user"??
"select * from user where user.nick = 'user001'");
IDataSet expected = new FlatXmlDataSet(new ClassPathResource(
"com/taobao/dbunit/dao/user001.xml").getFile());
Assertion.assertEquals(expected?? actual);
}
@Test
public void updateUser() throws Exception {
IDataSet origen = new FlatXmlDataSet(new ClassPathResource(
"com/taobao/dbunit/dao/user001.xml").getFile());
DatabaseOperation.INSERT.execute(conn?? origen);
User user = new User();
user.setNick("user001");
user.setPassword("password002");
userDao.update(user);
QueryDataSet actual = new QueryDataSet(conn);
actual.addTable("user"??
"select * from user where user.nick = 'user001'");
IDataSet expected = new FlatXmlDataSet(new ClassPathResource(
"com/taobao/dbunit/dao/user001_updated.xml").getFile());
Assertion.assertEquals(expected?? actual);
}
@Test
public void removeUser() throws Exception {
IDataSet origen = new FlatXmlDataSet(new ClassPathResource(
"com/taobao/dbunit/dao/user001.xml").getFile());
DatabaseOperation.INSERT.execute(conn?? origen);
userDao.remove("user001");
QueryDataSet actual = new QueryDataSet(conn);
actual.addTable("user"?? "select * from user where nick = 'user001'");
Assert.assertEquals(0?? actual.getTable("user").getRowCount());
}
@Test
public void findUser() throws Exception {
IDataSet data = new FlatXmlDataSet(new ClassPathResource(
"com/taobao/dbunit/dao/user001.xml").getFile());
DatabaseOperation.INSERT.execute(conn?? data);
User user = userDao.getUserByNick("user001");
Assert.assertEquals("password001"?? user.getPassword());
}
}
|
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
Spring Boot???????????SpringCloud????????Spring???????????????????Spring???????????Spring????????Spring???????????Spring??DAO?????????????Spring MVC?????????Spring MVC REST?????????????£?Spring MVC REST???????????????Spring TestContext???????Spring3 Security???????????Spring-Controller???????Spring.NET?ж?????????????Spring??????????ü???????spring cloud?????????ù?????????Spring?е???????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????