在spring中使用junit测试

1.需要导入的jar包:

spring-test-xxx.jar
junit.jar

2.在junit类前需要添加@RunWith和@ContextConfiguration两个注解

@RunWith 注释标签是 Junit 提供的,用来说明此测试类的运行者,这里用了 SpringJUnit4ClassRunner,这个类是一个针对 Junit 运行环境的自定义扩展,用来标准化在 Spring 环境中 Junit4.5 的测试用例,例如支持的注释标签的标准化。
@ContextConfiguration 注释标签是 Spring test context 提供的,用来指定 Spring 配置信息的来源,支持指定 XML 文件位置或者 Spring 配置类名,根据spring配置文件的位置,locations 有两种参数classpaths以及file。

@RunWith(SpringJUnit4ClassRunner.class)
如果spring配置文件在WEB-INF下,locations使用file
@ContextConfiguration(locations = {“file:WebRoot/WEB-INF/spring-mvc/dispatcher-servlet.xml”})
如果spring配置文件在src下,locations使用classpath
@ContextConfiguration(locations = {“classpath:/spring-mvc/dispatcher-servlet.xml”})

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = {"file:WebRoot/WEB-INF/spring-mvc/dispatcher-servlet.xml"}) 
public class plateDaoTest {

    @Resource
    private PlateDao plateDao;
    @Test
    public void test() {
        fail("Not yet implemented");
    }

    @Test
    public void addPlateTest(){
        Plate plate = new Plate();
        /**setter赋值*/
        plateDao.add(plate);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值