系列六、Spring整合单元测试

news2024/9/23 3:12:15

一、概述

        Spring中获取bean最常见的方式是通过ClassPathXmlApplicationContext 或者 AnnotationConfigApplicationContext的getBean()方式获取bean,那么在Spring中如何像在SpringBoot中直接一个类上添加个@SpringBootTest注解,即可在类中注入自己想要测试的bean呢?解决方案是有的,spring-test即提供了这个功能。Spring整合单元测试步骤如下:

二、Spring整合Junit单元测试

2.1、整体结构

2.2、pom

<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.star</groupId>
    <artifactId>spring5x06-mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring5x06-mybatis</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!--spring基本依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>

        <!-- 数据源 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.27</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.11</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.1.0</version>
        </dependency>

        <!-- 普通maven项目中使用Sl4j注解 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.32</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.10</version>
        </dependency>

        <!-- aop -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.19</version>
        </dependency>

        <!-- 工具 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.22</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.1</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

</project>

2.3、使用

/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/11/23 19:12
 * @Description: Spring整合单元测试
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpringJunitTest {

    @Resource
    private UserMapper userMapper;
    
    @Resource
    private UserService userService;

    @Test
    public void userMapperTest() {
        List<UserDO> userDOS = userMapper.listAllUser();
        System.out.println("userMapper = " + userMapper);
        System.out.println("userDOS = " + userDOS);
    }

    @Test
    public void userServiceTest() {
        List<UserDO> userDOS = userService.listAllUser();
        System.out.println("userService = " + userService);
        System.out.println("userDOS = " + userDOS);
    }

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1249075.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

JMeter 测试脚本编写技巧

JMeter 是一款开源软件&#xff0c;用于进行负载测试、性能测试及功能测试。测试人员可以使用 JMeter 编写测试脚本&#xff0c;模拟多种不同的负载情况&#xff0c;从而评估系统的性能和稳定性。以下是编写 JMeter 测试脚本的步骤。 第 1 步&#xff1a;创建测试计划 在JMet…

【Unity】EventSystem.current.IsPointerOverGameObject()对碰撞体起作用

本来我是用 EventSystem.current.IsPointerOverGameObject()来检测是否点击在UI上的&#xff0c;但是发现&#xff0c;他对我的碰撞体也是返回ture,研究半天。。。。找不出问题&#xff0c;然后发现我的相机上挂载了PhysicsRaycaster&#xff0c;去掉之后就好了&#xff0c;至于…

2014年全国硕士研究生入学统一考试管理类专业学位联考数学试题——解析版

文章目录 2014 年考研管理类联考数学真题一、问题求解&#xff08;本大题共 15 小题&#xff0c;每小题 3 分&#xff0c;共 45 分&#xff09;下列每题给出 5 个选项中&#xff0c;只有一个是符合要求的&#xff0c;请在答题卡上将所选择的字母涂黑。真题&#xff08;2014-01&…

蓝桥杯物联网竞赛_STM32L071_3_Oled显示

地位&#xff1a; 对于任何一门编程语言的学习&#xff0c;print函数毫无疑问是一种最好的调试手段&#xff0c;调试者不仅能通过它获取程序变量的运行状态而且通过对其合理使用获取程序的运行流程&#xff0c;更能通过关键变量的输出帮你验证推理的正确与否&#xff0c;朴素的…

12V降3.3V100mA稳压芯片WT7133

12V降3.3V100mA稳压芯片WT7133 WT71XX系列是一款采用CMOS工艺实现的三端高输入电压、低压差、小输出电流电压稳压器。 它的输出电流可达到100mA&#xff0c;输入电压可达到18V。其固定输出电压的范围是2.5V&#xff5e;8.0V&#xff0c;用户 也可通过外围应用电路来实现可变电压…

使用dbutil工具类查询数据表时,servlet传入sql数据 返回结果为null

使用dbutil工具类查询数据表时&#xff0c;servlet传入sql数据 返回结果为null 原本数据表中该有的数据却返回为空 解决办法&#xff1a; 在jdbc.properties配置文件中url连接里面加上utf-8字符集 urljdbc:mysql://localhost:3306/qfedu?useUnicodetrue&characterEncodi…

Maven - 打包之争:Jar vs. Shade vs. Assembly

文章目录 Pre概述Jar 打包方式_maven-jar-pluginOverview使用官方文档 Shade 打包方式_maven-shade-pluginOverview使用将部分jar包添加或排除将依赖jar包内部资源添加或排除自动将所有不使用的类排除将依赖的类重命名并打包进来 &#xff08;隔离方案&#xff09;修改包的后缀…

【合集一】每日一练30讲,轻松掌握Verilog语法

本原创教程由深圳市小眼睛科技有限公司创作&#xff0c;版权归本公司所有&#xff0c;如需转载&#xff0c;需授权并注明出处&#xff08;www.meyesemi.com) 第一练&#xff1a;如何区分&#xff1c;&#xff1d;表示的含义&#xff1f; 题目&#xff1a;请描述以下两种方法产…

什么年代了,你还不会自动化测试?

一、前言 在软件测试中&#xff0c;自动化测试指的是使用独立于待测软件的其他软件来自动执行测试、比较实际结果与预期并生成测试报告这一过程。在测试流程已经确定后&#xff0c;测试自动化可以自动执行的一些重复但必要测试工作。也可以完成手动测试几乎不可能完成的测试。…

葡萄酒怎么按照饮用时间分类?

不同的葡萄酒搭配不同的餐食&#xff0c;会让饮酒人有不一样的感受和体会&#xff0c;所以&#xff0c;葡萄酒是分场合并且有饮用时间的。云仓酒庄的品牌雷盛红酒分享一般按照饮用时间分类可以把葡萄酒分为三大类&#xff0c;分别是餐前酒、佐餐酒和餐后酒。 餐前酒&#xff1…

C++之模版初阶(简单使用模版)

前言 在学习C的模版之前&#xff0c;咱们先来说一说模版的概念&#xff0c;模版在我们的日常生活中非常常见&#xff0c;比如我们要做一个ppt&#xff0c;我们会去在WPS找个ppt的模版&#xff0c;我们只需要写入内容即可&#xff1b;比如我们的数学公式&#xff0c;给公式套值&…

Python监控服务进程及自启动服务方法与实践

1. 需求概述 当我们在Windows Server环境中部署XX系统的实际应用中&#xff0c;往往会遇到一些运维管理的挑战。为了确保系统的持续稳定运行&#xff0c;特别是在服务程序因各种原因突然关闭的情况下&#xff0c;我们可以借助Python的强大生态系统来构建一个监控与自动重启的管…

CBTC 2023氢能展倒计时6天,最新同期会议活动Plus版发布

随着时间的推移&#xff0c;CBTC2023深圳氢能技术展览会即将拉开序幕。这场盛会将于11月30日在深圳福田会展中心盛大开幕&#xff0c;以“以储赋能&#xff0c;智造未来”为主题&#xff0c;旨在搭建一个商务交流、供需合作、创新产品发布的平台&#xff0c;让氢能全产业链之间…

要想固态跑得稳,散热器也要够扎实,ORICO J-10 固态散热组合体验

我们在日常工作中&#xff0c;经常需要读写各种体积庞大的文件和数据&#xff0c;如果硬盘速度跟不上的话&#xff0c;工作效率就会大大降低。最近我发现M.2固态硬盘的价格已经大幅降低了&#xff0c;越来越多的国产品牌开始加入其中。所以我最近入手了一款ORICO J-10固态硬盘&…

HHDESK客户端连接导入导出

HHDESK客户端连接可以一键导出xlsx表格进行备份&#xff0c;也可一键导入设置连接。 1 导出 点击“资源”——“导出” 在弹出框中选择“保存”即可。 2 导入 右键“资源”——“导入” 点击“浏览”&#xff0c;选择相应的xlsx文档&#xff1b; 点击“确认”。 选择“…

一、TIDB基础

TIDB整个逻辑架构跟MYSQL类似&#xff0c;如下&#xff1a; TIDB集群&#xff1a;相当于MYSQL的数据库服务器&#xff0c;区别是MYSQL数据库服务器为单进程的&#xff0c;TIDB集群为分布式多进程的。 数据库&#xff1a;同MYSQL数据库&#xff0c;数据库属于集群&#xff0c;…

持续集成交付CICD:GitLabCI 通过trigger触发流水线

目录 一、理论 1.GitLabCI 二、实验 1.搭建共享库项目 2.GitLabCI 通过trigger触发流水线 三、问题 1.项目app02未触发项目app01 2.GitLab 报502网关错误 一、理论 1.GitLabCI (1) 概念 GitLab CI&#xff08;Continuous Integration&#xff09;是一种持续集成工具…

convertRect:toView 方法注意事项

这是在网上找到的一张图 我们开发中有时候会用到左边转换&#xff0c;convertRect:toView 通常情况下&#xff0c;我们回这样使用 CGRect newRect [a convertRect:originframe toView:c];其中newRect和 originframe的size相同&#xff0c;只改变origin newRect.origin a…

@ResponseBody详解

ResponseBody() 作用&#xff1a; responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后&#xff0c;写入到response对象的body区&#xff0c;通常用来返回JSON数据或者是XML数据。 位置&#xff1a; ResponseBody是作用在方法上的&…