SpringBoot整合Junit实例过程解析

发布时间:2020-10-11 05:40:06 作者:1572662
来源:脚本之家 阅读:171

这篇文章主要介绍了SpringBoot整合Junit实例过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

前提条件:SpringBoot已经整合了Mybatis,至于SpringBoot如何整合Mybatis可参考SpringBoot整合mybatis简单案例过程解析

SpringBoot为什么要整合Juni?

SpringBoot整合了Junit后,在写了Mapper接口后,可直接通过Junit进行测试,不用再写Controller层,不用启动引导类之后通过页面的形式一层一层的调用。

在SpringBoot整合Mybatis基础上,添加如下3步即可整合Junit并进行测试:

1、配置Junit的起步配置(pom.xml)

2、编写测试类

3、启动测试

具体内容如下:

pom.xml中配置Junit的起步配置

<!--SpringBoot集成Junit-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

编写测试类

package com.itheima;
import ch.qos.logback.core.net.SyslogOutputStream;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

//用哪个类来运行
@RunWith(SpringRunner.class)
//classes配置的是SpringBoot的引导类
@SpringBootTest(classes = SpringbootMybatisApplication.class)
public class MybatisTest {
  //注入自定义的要测试的Mapper接口
  @Autowired
  private UserMapper userMapper;

  //用于声明这是一个测试方法
  @Test
  public void test(){
    List<User> users = userMapper.queryUserList();
    System.out.println(users);
  }
}

注意该测试类是要写在src/test路径的某个文件夹中的,如下图:

SpringBoot整合Junit实例过程解析

在测试类的测试方法左侧点击启动按钮启动测试:

SpringBoot整合Junit实例过程解析

出现下图,代表执行成功,具体内容可在控制台查看:

SpringBoot整合Junit实例过程解析

以上内容为实验所的结果,若有理解不到位的地方,望指正及指点。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. Springboot整合junit的方法
  2. Spring整合junit的配置过程图解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot 整合

上一篇:Linux系统下为Nginx安装多版本PHP

下一篇:Python面向对象之静态属性、类方法与静态方法分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》