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可以阻止回滚