1、导包:test.jar
2、使用@RunWith注解创建spring容器 - @RunWith(SpringJUnit4ClassRunner.class)
3、使用@ContextConfiguration读取spring配置文件 - @ContextConfiguration("classpath:applicationContext.xml")
4、使用@Resource获取已装配bean的对象
package com.sikiedu.test; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.sikiedu.service.UserService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AopTest { // 将名为userService的对象注入到UserService中 @Resource(name = "userService") private UserService us; @Test public void test2() { us.delete(); } }