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);
}
}