Fork me on GitHub

Spring 框架整合JUnit单元测试

// 整合之前
public class Demo{

    @Test
    public void fun(){
        // 获取工厂,加载配置文件
        ApplicationContext ac = 
                new ClassPathXmlApplicationContext("applicationContext.xml");

        // 获取对象
        UserService us = (UserService)ac.getBean("userService");

        us.sayHello();
    }
}

// 整合步骤
// 要求: 必须先导入 JUnit4 的 jar包;
// 步骤一: 导入 spring-text.jar;
// 步骤二: 在具体的程序类上添加注解

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class Demo{

        @Resource(name="userService")
        private UserService userService;

        @Test
        public void fun(){
            userService.sayHello();
        }
    }

参考资料

posted @ 2017-10-23 20:20  小a的软件思考  阅读(159)  评论(0编辑  收藏  举报