前提:了解spring的相关配置。

需要使用 spring-test 的jar包。<!--more-->

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.Test;

//如果不需要事务,继承 AbstractTestNGSpringContextTests 即可
//新建了一个spring配置文件,配置了测试用的数据源,自动扫描的包路径或定义了需要使用的bean

@ContextConfiguration(locations = {"classpath*:spring/spring-test.xml"})
public class PrizeTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    @Qualifier(value = "prizeService")
    PrizeService prizeService;

    @Test(threadPoolSize = 100)
    @Rollback(false)  //默认为 true ,即执行完以后自动回滚。乌鸦需要执行完以后去数据库看结果,故设置false
    public void addTestAddValid() throws Exception {
        System.out.println(prizeService);
        Pool pool = new Pool();
        System.out.println(prizeService.addValid(pool));
    }

}