spring 单元测试 Junit

这篇具有很好参考价值的文章主要介绍了spring 单元测试 Junit。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

我是南城余!阿里云开发者平台专家博士证书获得者!

欢迎关注我的博客!一同成长!

一名从事运维开发的worker,记录分享学习。

专注于AI,运维开发,windows Linux 系统领域的分享!

本章节对应知识库

https://www.yuque.com/nanchengcyu/java

本内容来自尚硅谷课程,此处在知识库做了个人理解
————————————————

6、单元测试:JUnit

在之前的测试方法中,几乎都能看到以下的两行代码:

ApplicationContext context = new ClassPathXmlApplicationContext("xxx.xml");
Xxxx xxx = context.getBean(Xxxx.class);

这两行代码的作用是创建Spring容器,最终获取到对象,但是每次测试都需要重复编写。针对上述问题,我们需要的是程序能自动帮我们创建容器。我们都知道JUnit无法知晓我们是否使用了 Spring 框架,更不用说帮我们创建 Spring 容器了。Spring提供了一个运行器,可以读取配置文件(或注解)来创建容器。我们只需要告诉它配置文件位置就可以了。这样一来,我们通过Spring整合JUnit可以使程序创建spring容器了

6.1、整合JUnit5

6.1.1、搭建子模块

搭建spring-junit模块

6.1.2、引入依赖
<dependencies>
    <!--spring context依赖-->
    <!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.0.2</version>
    </dependency>

    <!--spring对junit的支持相关依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>6.0.2</version>
    </dependency>

    <!--junit5测试-->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.9.0</version>
    </dependency>

    <!--log4j2的依赖-->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.19.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j2-impl</artifactId>
        <version>2.19.0</version>
    </dependency>
</dependencies>
6.1.3、添加配置文件

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.atguigu.spring6.bean"/>
</beans>

copy日志文件:log4j2.xml

6.1.4、添加java类
package com.atguigu.spring6.bean;

import org.springframework.stereotype.Component;

@Component
public class User {

    public User() {
        System.out.println("run user");
    }
}
6.1.5、测试
import com.atguigu.spring6.bean.User;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

//两种方式均可
//方式一
//@ExtendWith(SpringExtension.class)
//@ContextConfiguration("classpath:beans.xml")
//方式二
@SpringJUnitConfig(locations = "classpath:beans.xml")
public class SpringJUnit5Test {

    @Autowired
    private User user;

    @Test
    public void testUser(){
        System.out.println(user);
    }
}

6.2、整合JUnit4

JUnit4在公司也会经常用到,在此也学习一下文章来源地址https://www.toymoban.com/news/detail-764734.html

6.2.1、添加依赖
<!-- junit测试 -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
6.2.2、测试
import com.atguigu.spring6.bean.User;
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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class SpringJUnit4Test {

    @Autowired
    private User user;

    @Test
    public void testUser(){
        System.out.println(user);
    }
}

到了这里,关于spring 单元测试 Junit的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java 单元测试Junit

    所谓 单元测试 ,就是针对最小的功能单元,编写测试代码对其进行正确性测试。为了测试更加方便,有一些第三方的公司或者组织提供了很好用的测试框架,给开发者使用。这里介绍一种Junit测试框架。Junit是第三方公司开源出来的,用于对代码进行单元测试的工具(IDEA已经

    2024年02月07日
    浏览(54)
  • Spring Cloud中Controller单元测试 Junit5 & MockMvc

    在Spring Cloud中进行Controller的单元测试,使用Junit5和Mock。 Controller: 方式一:使用@SpringBootTest + @AutoConfigureMockMvc 方式二:使用@WebMvcTest + @ImportAutoConfiguration(RefreshAutoConfiguration.class) 解决 No Scope registered for scope name \\\'refresh\\\' 异常 注入Mockmvc方式有两种 方式一:(@AutoConfigureMockMvc / @

    2024年02月16日
    浏览(31)
  • Spring Boot与微服务测试:JUnit和Mockito的单元和集成测试实践

    微服务架构的流行使得测试变得更为重要。在Spring Boot应用中,使用JUnit和Mockito进行单元和集成测试是一种常见的实践。本文将深入探讨如何利用这两个测试框架,确保Spring Boot微服务的可靠性和稳定性。 单元测试 1.1 JUnit简介 JUnit是Java中最为流行的单元测试框架之一,它提供

    2024年02月21日
    浏览(38)
  • Spring Boot使用JUnit和Mockito进行Service层单元测试

      在平时的开发当中,一个项目往往包含了大量的方法,可能有成千上万个。如何去保证这些方法产生的结果是我们想要的呢?那么在SpringBoot项目中我们是如何对Service层的方法进行测试的呢?   单元测试是一种软件测试方法,用于测试代码的最小可测试单元。在Java开发

    2024年02月03日
    浏览(44)
  • 单元测试-spring-boot-starter-test+junit5

    开发过程中经常需要写单元测试,记录一下单元测试spring-boot-starter-test+junit5的使用 引用jar包 可以对局部函数、方法进行调用测试 可以对API调用进行模拟测试

    2024年01月19日
    浏览(38)
  • Java IDEA JUnit 单元测试

    JUnit是一个开源的 Java 单元测试框架,它使得组织和运行测试代码变得非常简单,利用JUnit可以轻松地编写和执行单元测试,并且可以清楚地看到哪些测试成功,哪些失败 JUnit 还提供了生成测试报告的功能,报告不仅包含测试的成功率,还能统计被测试代码的覆盖率。通过进

    2024年02月03日
    浏览(30)
  • Java中的JUnit是什么?如何使用JUnit进行单元测试

    JUnit是Java中最流行的单元测试框架之一。它可以帮助开发人员在代码编写过程中检测出错误和异常,从而提高代码的质量和可靠性。 JUnit是一个由Kent Beck和Erich Gamma创建的开源Java单元测试框架,它已经成为Java开发中最常用的测试框架之一。这个框架的主要目的是为了简化单元

    2024年02月12日
    浏览(49)
  • Java单元测试浅析(JUnit+Mockito)

    Java测试我们应该都遇到过,一般我们会被要求做单元测试,来验证我们代码的功能以及效率。 这里来和大家一起探讨下有关单于测试。 是指对软件中的最小可测试单元进行检查和验证。对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义,如C语言中单

    2024年02月06日
    浏览(49)
  • Java单元测试 JUnit 5 快速上手

    一、背景 什么是 JUnit 5?首先就得聊下 Java 单元测试框架 JUnit,它与另一个框架 TestNG 占据了 Java领域里单元测试框架的主要市场,其中 JUnit 有着较长的发展历史和不断演进的丰富功能,备受大多数 Java 开发者的青睐。 而说到 JUnit 的历史,JUnit 起源于 1997年,最初版本是由两

    2024年02月11日
    浏览(29)
  • spring boot 单元测试JUnit5使用断言Assertions和假定Assumptions、嵌套、参数测试

    spring boot 单元测试JUnit5使用断言Assertions和假定Assumptions、嵌套、参数测试 本文基于spirng boot 2.7.11, 大家注意自己的版本 Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库 SpringBoot 2.4 以上版本移除了默认对 Vintage 的依赖 源码地址:https://gitcode.net/qq_39339588/springboot.git 1.

    2024年02月11日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包