第一步:引入Spring的测试包
实现:
package test;
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 spring.service.UserService;
/**
* @RunWith:指定Spring对junit提供的运行器
* @ContextConfiguration:指定Spring配置文件位置
*/
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath: ApplicationContext*.xml"})
public class TestSpring2 {
/**
* 在junit的bean中注入UserService
*/
@Autowired
UserService userService;
@Test
public void test2() {
userService.save();
}
}