Spring整合Junit详解

目录
  • 1,整合Junit4
  • 2,整合Junit5

1,整合Junit4

maven引入spring-test 和 junit4

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.2.22.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13</version>
                <scope>test</scope>
            </dependency>

在test目录下创建测试类

@RunWith(SpringJUnit4ClassRunner.class)  //启用Junit4
@ContextConfiguration("classpath:META-INF/context-junit.xml") //加载配置文件
public class SpringJunit4 {
    @Autowired
    private Machine machine;
    @Test
    public void test1() throws Exception {
        System.out.println(machine.getObject());
    }
}

2,整合Junit5

1,maven引入spring-test 和 junit5

JUnit 5 =JUnit Platform+JUnit Jupiter+JUnit Vintage

            <!-- junit 5 -->
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter</artifactId>
                <version>5.8.2</version>
                <scope>test</scope>
            </dependency>

2,在test目录下创建测试类

@ExtendWith(SpringExtension.class) //启用Junit5
@ContextConfiguration("classpath:META-INF/context-junit.xml") //加载配置文件
public class SpringJunit5 {
    @Autowired
    private Machine machine;
    @Test
    public void test1() throws Exception {
        System.out.println(machine.getObject());
    }
}

说明:在spring5中,可以用复合注解 @SpringJUnitConfig(locations = {"classpath:META-INF/context-junit.xml"}) 代替@ExtendWith(SpringExtension.class) 和@ContextConfiguration("classpath:META-INF/context-junit.xml")

//@ExtendWith(SpringExtension.class) //启用Junit5
//@ContextConfiguration("classpath:META-INF/context-junit.xml") //加载配置文件
@SpringJUnitConfig(locations = {"classpath:META-INF/context-junit.xml"})
public class SpringJunit5 {
    @Autowired
    private Machine machine;
    @Test
    public void test1() throws Exception {
        System.out.println(machine.getObject());
    }
}

到此这篇关于Spring整合Junit详解的文章就介绍到这了,更多相关Spring Junit内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • SpringMVC框架如何与Junit整合看这个就够了

    目录 系统环境 引入依赖 编写单元测试基类 MockMvcRequestBuilders get请求测试 post请求测试 文件上传测试 返回结果是视图请求的单元测试 直接测试一个service的方法 总结 系统环境 软件 版本 spring-webmvc 4.3.6.RELEASE spring-test 4.3.6.RELEASE junit 4.12 引入依赖 <dependency> <groupId>junit</groupId> <artifactId

  • Springboot整合junit过程解析

    对maven项目的pom.xml进行配置 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <!--junit5不需要配置junit-vintage-engine--> &

  • Spring整合Junit的使用详解

    我们在编写完Spring的代码后,往往需要测试代码的正确性,这个时候就需要用到单元测试了.我们这里使用的版本是junit4. 一个程序的入口是main方法,但是junit中不存在main方法,是因为junit内部的原理是它自己内部就有个main方法,运行扫描带@Test注解的方法,然后反射调用该方法,完成测试. 调用Spring框架的测试代码: @Test public void function(){ ApplicationContext applicationContext = new Cl

  • SpringBoot整合Junit实例过程解析

    这篇文章主要介绍了SpringBoot整合Junit实例过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 前提条件:SpringBoot已经整合了Mybatis,至于SpringBoot如何整合Mybatis可参考SpringBoot整合mybatis简单案例过程解析 SpringBoot为什么要整合Juni? SpringBoot整合了Junit后,在写了Mapper接口后,可直接通过Junit进行测试,不用再写Controller层,

  • Spring整合junit的配置过程图解

    配置步骤: 1.导入Spring整合Junit的jar(坐标): <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> <scope>test</scope> </dependency> 2.使用Ju

  • Java Spring详解如何配置数据源注解开发以及整合Junit

    目录 Spring数据源的配置 数据源(连接池)的作用 数据源的开发步骤 手动创建数据源 Spring注解开发 Spring原始注解 Spring新注解 Spring整合Junit Spring集成Junit步骤 Spring数据源的配置 数据源(连接池)的作用 数据源(连接池)是提高程序性能如出现的 事先实例化数据源,初始化部分连接资源 使用连接资源时从数据源中获取 使用完毕后将连接资源归还给数据源 常见的数据源(连接池):DBCP.C3PO.BoneCP.Druid等 数据源的开发步骤 1.

  • Junit写法及与spring整合过程详解

    junit之前的写法: //在Before中注入service类 private IUserService userService; @Before public void setUp() throws Exception { //使用xml的方式 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //使用注解的方式 Applicat

  • 解决Spring boot 整合Junit遇到的坑

    目录 这是我在使用springboot整合Junit的时候遇到的坑 1.在pom.xml中添加junit环境的依赖 2.在src/test/java下建立测试类 3.自己编写的启动类 SpringBoot 整合Junit测试注入Bean失败 问题描述 下面是我的测试类 解决过程 以下是我的启动类 总结 这是我在使用springboot整合Junit的时候遇到的坑 1.在pom.xml中添加junit环境的依赖 <dependency> <groupId>org.springfram

  • SpringMVC框架整合Junit进行单元测试(案例详解)

    本文主要介绍在SpringMVC框架整合Junit框架进行单元测试.闲话少述,让我们直入主题. 系统环境 软件 版本 spring-webmvc 4.3.6.RELEASE spring-test 4.3.6.RELEASE junit 4.12 引入依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</ver

  • Spring整合Junit详解

    目录 1,整合Junit4 2,整合Junit5 1,整合Junit4 maven引入spring-test 和 junit4 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.22.RELEASE</version> </dependency> <de

  • 如何优雅的进行Spring整合MongoDB详解

    前言 本文重点是要将mongodb与spring整合到项目中去,在实践中发现问题,追踪问题,然后解决问题.下面话不多说了,来一起看看详细的介绍吧. 一.准备 Maven.Spring(spring-data-mongodb) spring Data for MongoDB是Spring Data的一个子模块. 目标是为mongodb提供一个相近的一致的基于Spring的编程模型. Spring Data for MongoDB核心功能是映射POJO到Mongo的DBCollection中的文档,

  • spring Boot与Mybatis整合优化详解

    SpringBoot官方文档http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ 关于spring-boot与mybatis整合优化方面的介绍,就是Mybatis-Spring-boot-starter的介绍: 1.取消spring-mybatis.xml配置 ①自动检测已存在的Datasource 之前,需要在spring-mybatis.xml中配置datasource的Bean,现在只需要在applicat

  • Spring Boot示例代码整合Redis详解

    目录 Redis 简介 Redis 优势 Redis与其他key-value存储有什么不同 添加Redis依赖包 配置Redis数据库连接 编写Redis操作工具类 测试 Redis 简介 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. Redis不仅仅支持简单的key-value类型的数据

  • Spring boot整合security详解

    目录 前言 配置依赖 用户配置 1.内存用户存储 2.数据库用户存储 3.LDAP用户存储 4.自定义用户存储 拦截配置 前言 在进行框架选型时最常用的选择就是在Spring security 和Shiro中进行抉择,Spring security 和 shiro 一样,都具有认证.授权.加密等用于权限管理的功能.但是对于Springboot而言,Spring Security比Shiro更合适一些,他们都是Spring生态里的内容,并且在使用上Spring boot只需要引入Security就

  • Spring Boot整合Thymeleaf详解

    目录 Thymeleaf 基本介绍 基本语法 th:text文本替换 th:if和th:unless文本替换 th:each foreach循环 th:id.th:value.th:checked等(和form表单相关) 整合Thymeleaf 基本配置 三层架构 删除操作 编辑操作 用户登录 用户注销 点击注销用户 Thymeleaf 基本介绍 Spring Boot 官方推荐使用 Thymeleaf 作为其模板引擎.SpringBoot 为 Thymeleaf 提供了一系列默认配置,并且为T

  • springboot与mybatis整合实例详解(完美融合)

    简介 从 Spring Boot 项目名称中的 Boot 可以看出来,Spring Boot 的作用在于创建和启动新的基于 Spring 框架的项目.它的目的是帮助开发人员很容易的创建出独立运行和产品级别的基于 Spring 框架的应用.Spring Boot 会选择最适合的 Spring 子项目和第三方开源库进行整合.大部分 Spring Boot 应用只需要非常少的配置就可以快速运行起来. Spring Boot 包含的特性如下: 创建可以独立运行的 Spring 应用. 直接嵌入 Tomc

  • Spring集成Redis详解代码示例

    本文章从头开始介绍Spring集成Redis的示例. Eclipse工程结构 如下图为我的示例工程的结构图,采用Maven构建.其中需要集成Spring,因此需要beans.xml文件配置spring的依赖注入,redis.properties配置连接服务器的配置信息. 其中工程中beans.xml和redis.properties文件直接放在了根目录,有需要的读者可以放到resource目录中. POM依赖 如下为示例POM依赖,Spring集成redis需要依赖的包为:jedis包,spri

  • spring缓存代码详解

    本文研究的主要是spring缓存的相关内容,具体介绍如下. 这篇文章是根据谷歌翻译大致修改出来的,由于原文不知道是什么语,所以可能导致翻译的有错误和不准确的地方,但是大致的方向感觉还是蛮不错的,所以在这里整理了一下,希望能够有所帮助. 高速缓存一直是一个非常需要这两个提高应用程序性能并降低其工作量.此外,它的用处今天是特别明显,可以作出处理成千上万的游客concurrents.D'un架构上的Web应用,高速缓存管理正交于应用程序的业务逻辑和出于这个原因,应该对应用程序本身的发展产生的影响最小.

随机推荐