自己是如何使用单元测试

前言

自己是如何使用单元测试

进行单元测试能够让我们在编写方法的具体实现代码后,能清晰地看到其是否能实现预期的功能,有助于我们及时修正自己方法中存在的bug,以免在后续使用到某方法时出现意想不到的错误。


一、引入单元测试所使用的依赖

复制代码
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
            	<!---如果不需要junit4就排除该依赖->
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

		<dependency>
            <groupId>org.jeasy</groupId>
            <artifactId>easy-random-core</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
            <exclusions>
                <!-- 跟 SpringAOP 引入的 objenesis 有冲突,要排除 -->
                <exclusion>
                    <groupId>org.objenesis</groupId>
                    <artifactId>objenesis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <!-- 支持根据参数校验逻辑生成对象字段 -->
        <dependency>
            <groupId>org.jeasy</groupId>
            <artifactId>easy-random-bean-validation</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <artifactId>snakeyaml</artifactId>
                    <groupId>org.yaml</groupId>
                </exclusion>
            </exclusions>
        </dependency>

二、如何在Controller层进行单元测试?

代码如下(示例):

复制代码
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@AutoConfigureMockMvc
class xxxTest{
	@Autowired
    private MockMvc mockMvc;
	
	Headers headers = new Headers();
	
	@BeforeEach
	void setUp(){
		// TODO 如果需要Token等内容可以在测试之前准备好
	}
	
	@Test
	void testXxx(){
		// 准备好Controller层所使用的参数
		// 通过使用EasyRandom类生成随机参数
		XxxVO vo = new EasyRandom().nextObject(XxxVO.class)
		// 将参数转换为Json字符串
		String content = JSONObject.toJSONString(vo);
		// 发送请求
		String contentAsString = mockMvc.perform(
				// 发送Post请求
				MockMvcRequestBuilders.post(url)
				// 在header中添加参数,并设置编码
		       .headers(headers).contentType(MediaType.APPLICATION_JSON_VALUE)
		        // 发送的Json内容
		       .content(content.getBytes(StandardCharsets.UTF_8)).andReturn()
		       .getResponse().getContentAsString(StandardCharsets.UTF_8);
		System.out.println(contentAsString);
	}
}

三、如何在Service层进行单元测试?

代码如下(示例):

复制代码
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("dev")
class XxxTest(){
	@Autowried
	private XxxService xxxService;

	@Test
	@DisplayName("测试某个功能")
	// 关闭该测试
	@Disabled
	void testXxx(){
		
	}
}

总结

用JUnit编写测试类,进行单元测试能够让我们在编写方法的具体实现代码后,能清晰地看到其是否能实现预期的功能,有助于我们及时修正自己方法中存在的bug,以免在后续使用到某方法时出现意想不到的错误。

最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取**【保证100%免费】**
软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

行动吧,在路上总比一直观望的要好,未来的你肯定会感谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时点击加入群:1150305204,里面有各种测试开发资料和技术可以一起交流哦。

相关推荐
川石课堂软件测试1 小时前
自动化测试之 Cucumber 工具
数据库·功能测试·网络协议·测试工具·mysql·单元测试·prometheus
执剑、天涯7 小时前
通过一个typescript的小游戏,使用单元测试实战(二)
javascript·typescript·单元测试
啊森要自信8 小时前
【GUI自动化测试】Python 自动化测试框架 pytest 全面指南:基础语法、核心特性(参数化 / Fixture)及项目实操
开发语言·python·ui·单元测试·pytest
川石课堂软件测试17 小时前
MySQL数据库之DBA命令
数据库·网络协议·mysql·http·单元测试·prometheus·dba
lifewange1 天前
幂等机制
功能测试·单元测试
=>>漫反射=>>2 天前
单元测试 vs Main方法调试:何时使用哪种方式?
java·spring boot·单元测试
子正2 天前
Pytest单元测试一例:u16采样值格式转换的错误
单元测试·pytest
另寻沧海3 天前
测试中的 AAA 模式与 Given–When–Then 模式详解
c++·单元测试·测试覆盖率
寒月霜华3 天前
java-高级技术(单元测试、反射)
java·开发语言·单元测试·反射
阿杰真不会敲代码3 天前
junit单元测试
junit·单元测试