1.添加依赖
- spring-test
- junit
- spring-context(自动添加依赖其他所需的spring依赖包)
2.在class前添加以下注解,用于配置xml文件的位置
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application.xml")
3.测试方法上方加入 @Test
spring整合Test功能与原Test功能区别:不需要再getBean获取容器对象,直接通过注解方式注入对象
1 package com.web.test; 2 3 import javax.annotation.Resource; 4 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.test.context.ContextConfiguration; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 10 @RunWith(SpringJUnit4ClassRunner.class) 11 @ContextConfiguration(locations="classpath:application.xml") 12 public class SpringTest { 13 @Resource 14 One one; 15 @Test 16 public void test1(){ 17 one.playing(); 18 } 19 20 21 }