Spring框架入门个人笔记Ioc之Spring整合junit的配置

对应工程heima-annoioc-withoutxml

上文提到说junit存在着大量重复代码,如下进行修改:

public class AccountServiceTest {
    private ApplicationContext ac = null;
    private IAccountService as = null;
    
    @Before
    public void init() {
        //        1.获取容器,此时采用被注解过的类
        ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//        2.获取业务层对象
        as = ac.getBean("accountService", IAccountService.class);
    }

    @Test
    public void testFindAll() {
//        3.执行方法
        List<Account> allaccounts = as.findAllAccount();
        for (Account aaa : allaccounts) {
            System.out.println(aaa);
        }
    }

    @Test
    public void testFindOne() {
//        3.执行方法
        Account oneaccount = as.findAccountById(1);
        System.out.println(oneaccount);
    }

    @Test
    public void testSave() {
//        出现报错说id没有默认值,那就在数据库里面把id弄成自增
        Account saveaccount = new Account();
        saveaccount.setName("水野朝阳");
        saveaccount.setMoney(1000f);
//        3.执行方法
        as.saveAccount(saveaccount);
    }

    @Test
    public void testUpdate() {
//        3.执行方法
        Account updateaccount = as.findAccountById(2);
        updateaccount.setMoney(1000f);
        as.updateAccount(updateaccount);
    }

    @Test
    public void testDelete() {
//        3.执行方法
        as.deleteAccount(6);
    }
}

还有一种方法就是采用Spring整合junit的配置:

  1. 导入spring整合Junit的jar
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
  1. 使用Junit提供的一个注解把原有的main方法替换了,换成spring提供的@Runwith。例子如下:
@RunWith(SpringJUnit4ClassRunner.class)
public class AccountServiceTest {}
  1. 告知spring的运行器,spring和ioc创建的基于xml还是注解的,并说明位置。
    @ContextConfiguration 属性:
    locations : 指定xml位置,加上classpath关键字,表示在类路径下
    classes: 指定配置类所在位置
    例子如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {}

总结:当使用spring 5.X版本的时候,要求junit的jar必须是4.12以上。
整体代码如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {

    @Autowired
    private IAccountService as = null;

    @Test
    public void testFindAll() {
//        3.执行方法
        List<Account> allaccounts = as.findAllAccount();
        for (Account aaa : allaccounts) {
            System.out.println(aaa);
        }
    }

    @Test
    public void testFindOne() {
//        3.执行方法
        Account oneaccount = as.findAccountById(1);
        System.out.println(oneaccount);
    }

    @Test
    public void testSave() {
//        出现报错说id没有默认值,那就在数据库里面把id弄成自增
        Account saveaccount = new Account();
        saveaccount.setName("水野朝阳");
        saveaccount.setMoney(1000f);
//        3.执行方法
        as.saveAccount(saveaccount);
    }

    @Test
    public void testUpdate() {
//        3.执行方法
        Account updateaccount = as.findAccountById(2);
        updateaccount.setMoney(1000f);
        as.updateAccount(updateaccount);
    }

    @Test
    public void testDelete() {
//        3.执行方法
        as.deleteAccount(6);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值