Spring--整合Junit,你觉得你真的了解Kafka消费者吗

该博客介绍了如何通过Spring整合JUnit来减少测试模块中的重复代码。首先,需要导入Spring整合JUnit的相关jar包,然后使用@Runwith替换原有的main方法,并通过@ContextConfiguration指定XML配置文件的位置。通过这种方式,可以避免在每个测试方法中都手动创建ApplicationContext。测试过程中,展示了从获取容器到执行业务方法的步骤,包括保存、查找、更新和删除操作。注意,使用Spring 5.x版本时,Junit版本需为4.12或更高。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");

    //2.得到业务层对象

    IAccountService as=ac.getBean("accountService",IAccountService.class);

    //3.执行方法

    List<Account> accounts=as.findAllAccount();

    for(Account account:accounts){

        System.out.println(account);

    }

}

@Test

public void testFindOne(){

    //1.获取容器

    ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");

    //2.得到业务层对象

    IAccountService as=ac.getBean("accountService",IAccountService.class);

    //3.执行方法

    Account account=as.findAccountById(1);

    System.out.println(account);

}

@Test

public void testSave(){

    Account account=new Account();

    account.setName("sss");

    account.setMoney(123f);

    //1.获取容器

    ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");

    //2.得到业务层对象

    IAccountService as=ac.getBean("accountService",IAccountService.class);

    //3.执行方法

    as.saveAccount(account);



}

@Test

public void testUpdate(){

    //1.获取容器

    ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");

    //2.得到业务层对象

    IAccountService as=ac.getBean("accountService",IAccountService.class);

    //3.执行方法

    Account account=as.findAccountById(4);

    account.setMoney(2131f);

    as.updateAccount(account);



}

@Test

public void testDelete(){

    //1.获取容器

    ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");

    //2.得到业务层对象

    IAccountService as=ac.getBean("accountService",IAccountService.class);

    //3.执行方法

    as.deleteAccount(4);

}

}




在上边代码中中我们可以看出,每一个测试模块中都有着重复的代码:  

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200910163227798.png#pic_left)



**采用Spring整合Junit:**



相关配置:



*   `1. 导入spring整合junit的jar包(坐标)`

*   `2. 使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的@Runwith`

*   `3. 告知spring的运行器,spring和ioc创建是基于xml或注解的,并且说明位置`

    *   @ContextConfiguration:  

        Location:指定xml文件的位置,加上classpath关键字,表示在类路径下  

        classes:指定注解所在的位置



注意:当我们使用spring 5.x版本的时候,要求junit版本必须是4.12以上



1、在pom.xml中导入spring整合junit的jar包



<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=“http://maven.apache.org/POM/4.0.0”

     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>



<groupId>org.example</groupId>

<artifactId>spring_day02_account_anno_withoutxml</artifactId>

<version>1.0-SNAPSHOT</version>

最后

在面试前我整理归纳了一些面试学习资料,文中结合我的朋友同学面试美团滴滴这类大厂的资料及案例
**[CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】](

)**

MyBatis答案解析
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

大家看完有什么不懂的可以在下方留言讨论也可以关注。

觉得文章对你有帮助的话记得关注我点个赞支持一下!

G-1631181765439)]

[外链图片转存中…(img-8qZTjou5-1631181765441)]
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

大家看完有什么不懂的可以在下方留言讨论也可以关注。

觉得文章对你有帮助的话记得关注我点个赞支持一下!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值