Spring与Dao-Jdbc模板-使用Spring的JUnit4测试
注意:使用Spring的JUnit4测试需要在搭建好的环境中再添加两个jar,spring-aop-4.2.1.RELEASE.jar
和spring-test-4.2.1.RELEASE.jar
package com.hk.springdao.test; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.hk.springdao.beans.Student; import com.hk.springdao.service.IStudentService; /** * * @author 浪丶荡 * */ @RunWith(SpringJUnit4ClassRunner.class)//运行环境 @ContextConfiguration(locations="classpath:applicationContext.xml") public class MyTest_spring { @Autowired private IStudentService studentService; @Test public void testAdd(){ Student stu = new Student("浪丶荡",18); studentService.addStudent(stu ); } @Test public void testRemove(){ studentService.removeStudent(2); } @Test public void testModify(){ Student stu = new Student(); stu.setId(3); stu.setAge(10); stu.setName("黑旋风"); studentService.modifyStudent(stu); } }