转账_整合Junit

l  导入jar包

       基本 :4+1

       测试:spring-test...jar

1.让Junit通知spring加载配置文件

2.让spring容器自动进行注入

首先,我们需要修改测试类,在原有的基础上,让其不需要在类中使用spring容器

我们在上篇博文中将测试类修改为如下代码,其他类以及.xml文件均不做修改,也可以实现转账功能

package com.itheima;


import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.itheima.service.impl.AccountService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestApp {
    @Autowired  //与junit整合,不需要在spring xml配置扫描
    private AccountService accountService;
    @Test
    public void demo01(){
//        //获得容器
//        String xmlPath = "applicationContext.xml";
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
//        //获得容器内容,不需要自己new,都是从spring中获得,applicationContext.getBean("accountService")指的是id
//        AccountService accountService =  (AccountService) applicationContext.getBean("accountService");
        accountService.transfer("jack", "rose", 1000);
    }

}
原文地址:https://www.cnblogs.com/zjf-293916/p/7443939.html