Spring Boot—(11)SpringBoot使用Junit单元测试

Spring Boot—(11)SpringBoot使用Junit单元测试欢迎关注公众号:java4all摘要:本文详细的记录了SpringBoot如何结合Junit写测试用例,如何执行,打包执行,忽略执行等操作,SpringBoot内置了Junit测试组件,使用很方便,不用再单独引入其他测试组件。演示环境:SpringBoot+mybatis开发工具:IntelliJIDEA1.pom.xml一般使用idea新建一个Sp…

大家好,又见面了,我是你们的朋友全栈君。

摘要:本文详细的记录了SpringBoot如何结合Junit写测试用例,如何执行,打包执行,忽略执行等操作,SpringBoot内置了Junit测试组件,使用很方便,不用再单独引入其他测试组件。

 

演示环境:

SpringBoot + mybatis

开发工具:IntelliJ IDEA

 

1.pom.xml

 

一般使用idea新建一个SpringBoot web项目时,一般都会自动引入此依赖,如果没有,请手动引入。

 

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

 

2.测试类基类

 

新建的项目,一般会有test包和test类,结构如下:

Spring Boot---(11)SpringBoot使用Junit单元测试

如果没有,我们自己创建一个,由于一个项目中我们会写很多很多测试类,而测试类上面是需要以下几个注解的,每建一个类都去补注解,太麻烦,我们就在这个类中加上注解,其他测试类直接继承这个类就好了:

 

package com.alibaba;

import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringRunner.class)
@SpringBootTest
//由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration
public class TmallApplicationTests {

    @Before
    public void init() {
        System.out.println("开始测试-----------------");
    }

    @After
    public void after() {
        System.out.println("测试结束-----------------");
    }
}

 

3.controller,service,dao等,省略,就是普通方法,普通接口

4.测试类

 

我这里建一个测试类,继承基类,然后测试我service中的两个方法

 

package com.alibaba;

import com.alibaba.service.EntFileService;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by lightClouds917
 * Date 2018/2/2
 * Description:测试类
 */
public class EntFileTest extends TmallApplicationTests {


    @Autowired
    private EntFileService entFileService;
    //@Ignore("not ready yet")
    @Test
    public void testGetEntFileById(){
        Assert.assertSame("企业数量有误",500,entFileService.getCount());
    }

    @Test
    public void testGetEntFileList(){
        Assert.assertSame("企业数量不为10",10,entFileService.getEntFileList());
    }
}

如上,直接@Autowired引入你想测试的类就好,然后继承基类,测试方法上面要加@Test注解。

然后,第一个测试方法:我想测试一下企业数量是不是600,参数意义:

第一个参数:如果测试不通过,会抛出此消息,此参数可不要;

第二个参数:我预期的值,我这里希望他查出来的结果是600;

第三个参数:是实际的结果,就是我们调用方法返回的结果;

我们可以看一下Assert类的源码:
 

    /**
     * Asserts that two objects refer to the same object. If they are not, an
     * {@link AssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
     * okay)
     * @param expected the expected object
     * @param actual the object to compare to <code>expected</code>
     */
    static public void assertSame(String message, Object expected, Object actual) {
        if (expected == actual) {
            return;
        }
        failNotSame(message, expected, actual);
    }

    /**
     * Asserts that two objects refer to the same object. If they are not the
     * same, an {@link AssertionError} without a message is thrown.
     *
     * @param expected the expected object
     * @param actual the object to compare to <code>expected</code>
     */
    static public void assertSame(Object expected, Object actual) {
        assertSame(null, expected, actual);
    }

 

5.运行测试用例

 

运行有两种方法:

1.选中方法,右键,然后run 。。。;

2.点击方法前的小标;

具体操作如下截图:

Spring Boot---(11)SpringBoot使用Junit单元测试

现在看运行结果,如下图:

区块一:这里是测试用例的执行结果,由于未获得预期结果,打印出了我们提前设置的错误信息。

区块二:这是测试用例的覆盖率,类的覆盖,方法的覆盖,行数的覆盖,非常详细。

区块三:此区块是预期结果和实际结果的详细对比,点击后才会显示,如图点击位置。

Spring Boot---(11)SpringBoot使用Junit单元测试

关于Assert中,还有很多断言方法,方法名字很规范,看名字就知道怎么用了,这里不再过多说明。

 

6.打包测试

 

项目开发完后,我们写了100个测试用例类,我不能每个类都点击进去,然后慢慢执行,SpringBoot提供了打包测试的方式:我们用一个类,把所有的测试类整理进去,然后直接运行这个类,所有的测试类都会执行。

我这里建了两个测试类,分别是EntFileTest,EntFileTest2,现在我打包进TestSuits,让他们一次运行:

 

@Suite.SuiteClasses({EntFileTest.class,EntFileTest2.class})

Spring Boot---(11)SpringBoot使用Junit单元测试

打包完整代码:

 

package com.alibaba;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

/**
 * Created by lightClouds917
 * Date 2018/2/2
 * Description:打包测试
 */
//@Ignore("not ready yet")
@RunWith(Suite.class)
@Suite.SuiteClasses({EntFileTest.class,EntFileTest2.class})
public class TestSuits {

    //不用写代码,只需要注解即可
}

 

 

7.忽略方法

 

当我一个测试类写了10个测试方法时,其中有1个我暂时不想测,想跳过,但是其他9个我想一次运行,怎么办?这里有一个忽略注解,写在方法上,可以忽略这个测试方法,写在类上,可以忽略这个类。

 

package com.alibaba;

import com.alibaba.service.EntFileService;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by lightClouds917
 * Date 2018/2/2
 * Description:测试类
 */
public class EntFileTest extends TmallApplicationTests {


    @Autowired
    private EntFileService entFileService;

    @Ignore("not ready yet")
    @Test
    public void testGetEntFileById(){
        Assert.assertSame("企业数量有误",600,entFileService.getCount());
    }

    @Test
    public void testGetEntFileList(){
        Assert.assertSame("企业数量不为10",10,entFileService.getEntFileList());
    }
}

此时,运行这个测试类,testGetEntFileById()方法就会忽略执行。

—————————-猝不及防,我要打广告了—————————-

欢迎关注公众号java4all,或添加IT云清个人号,一起聊技术聊成长。

Spring Boot---(11)SpringBoot使用Junit单元测试   Spring Boot---(11)SpringBoot使用Junit单元测试

            java4all 公众号                       IT云清 个人号 

—————————-猝不及防,我要打广告了—————————-

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/133166.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 平行运算:Parallel.For、Parallel.Foreach的体验式试用[通俗易懂]

    平行运算:Parallel.For、Parallel.Foreach的体验式试用[通俗易懂]在编程里面我们经常会遇到编历一个列表或数组做同一件事情或操作,当这个数组或列表很大时又或是需要进行很复杂的操作时,就会花费很长的时间。以前我就在想能不能在这种情况下使用多线程的方式提高效率,可惜一直都没机会和动力(实际需要)去研究。今天在网上查找资料,很偶然的发现.NETFramework4.0中平行算法相关内容(Parallel.For、Parallel.Foreach),原来.NET已经实

    2022年7月19日
  • 解析为什么hashmap是线程不安全的?「建议收藏」

    解析为什么hashmap是线程不安全的?「建议收藏」扩容一般我们声明HashMap时,使用的都是默认的构造方法:HashMap<K,V>,看了代码你会发现,它还有其它的构造方法:HashMap(intinitialCapacity,floatloadFactor),其中参数initialCapacity为初始容量,loadFactor为加载因子,扩容就是在put加入元素的个数超过initialCapacity*loa…

    2022年10月11日
  • 滴滴开源敏捷测试用例管理平台!

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 简介 AgileTC是一套敏捷的测试用例管理平台,具备与xmind等脑图工具一致的操作体验。支持测试用例管理、执行计…

    2021年6月25日
  • Unity 安装失败原因

    Unity 安装失败原因文件路径中含有中文,会乱码,导致安装失败\color{red}{文件路径中含有中文,会乱码,导致安装失败}文件路径中含有中文,会乱码,导致安装失败把文件路径换成没有中文的就好了!\color{red}{把文件路径换成没有中文的就好了!}把文件路径换成没有中文的就好了!…

    2022年6月27日
  • 【VS开发】socket编程原理

    【VS开发】socket编程原理socket编程原理1、问题的引入1)普通的I/O操作过程:UNIX系统的I/O命令集,是从Maltics和早期系统中的命令演变出来的,其模式为打开一读/写一关闭(open-write-read-close)。在一个用户进程进行I/O操作时,它首先调用“打开”获得对指定文件或设备的使用权,并返回称为文件描述符的整型数,以描述用户在打开的文件或设备上

    2022年10月17日
  • jenkins踩坑——插件冲突「建议收藏」

    jenkins踩坑——插件冲突「建议收藏」当前jenkins版本:2.286冲突插件:FoldersPlugin和ExtendedChoiceParameterPlug-In问题现象通过FoldersPlugin创建目录层次的工程某一个工程创建之后无法读取文件中的内容,而这个工程是从同一目录的其他工程(可以读取文件内容)复制而来。仅项目名称和svn路径不同问题分析增加插件com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParam

    2022年5月7日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号