Junit加载Spring容器作单元测试_添加事务回滚
在执行单元测试时,为了避免产生脏数据,将测试单元设置成事务回滚!!!!!
-
package com.pyy.sell.dao; import com.pyy.sell.po.ProductCategory; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; /** * 类目 * Created by pyy * 2017年12月21日16:07:52 */ @RunWith(SpringRunner.class) @SpringBootTest @Transactional // 默认rollback=true public class ProductCategoryDaoTest { @Autowired private ProductCategoryDao productCategoryDao; @Test public void saveTest(){ ProductCategory productCategory = new ProductCategory(); productCategory.setCategoryName("热销产品"); productCategory.setCategoryType(1); productCategoryDao.save(productCategory); } @Test public void updateTest(){ ProductCategory productCategory = productCategoryDao.findOne(1); productCategory.setCategoryId(1); productCategory.setCategoryName("热销产品2"); productCategory.setCategoryType(1); ProductCategory result = productCategoryDao.save(productCategory); Assert.assertNotNull(result); } @Test public void findByTest(){ ProductCategory productCategory = productCategoryDao.findOne(1); Assert.assertNotNull(productCategory); } }