Sping TestNG 启用事物@Transactional
Posted ff111
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sping TestNG 启用事物@Transactional相关的知识,希望对你有一定的参考价值。
import org.mockito.MockitoAnnotations; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests; import org.testng.annotations.BeforeClass; @ContextConfiguration(locations = {"classpath*:conf/spring/spring-context-test.xml"})//配置文件加载 public abstract class BaseTestNG extends AbstractTransactionalTestNGSpringContextTests { public static final Logger LOGGER = LoggerFactory.getLogger(BaseTestNG.class); public abstract void initMock(); @BeforeClass(alwaysRun = true) public void init() { MockitoAnnotations.initMocks(this); this.initMock(); } }
记得继承:AbstractTransactionalTestNGSpringContextTests
public class CategoryServiceImplTest extends BaseTestNG { @Autowired CategoryService categoryService; @Test(priority = 100) @Rollback(value = true) public void testCreateAdminPlatformCategory() { CreateAdminPlatformCategoryInDTO createAdminPlatformCategoryInDTO = new CreateAdminPlatformCategoryInDTO(); createAdminPlatformCategoryInDTO.setCategoryName("单元测试001"); createAdminPlatformCategoryInDTO.setOperatorUser("17093352"); LOGGER.info("输出:{}", categoryService.createAdminPlatformCategory(createAdminPlatformCategoryInDTO)); }
}
@Rollback(value = true) value=false可以阻止回滚
以上是关于Sping TestNG 启用事物@Transactional的主要内容,如果未能解决你的问题,请参考以下文章