自己是如何使用单元测试

前言

自己是如何使用单元测试

进行单元测试能够让我们在编写方法的具体实现代码后,能清晰地看到其是否能实现预期的功能,有助于我们及时修正自己方法中存在的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,里面有各种测试开发资料和技术可以一起交流哦。

相关推荐
gb421528720 小时前
单元测试报错
单元测试·log4j
蔡蓝2 天前
设计模式-单一职责原则
设计模式·log4j·单一职责原则
EndingCoder3 天前
React从基础入门到高级实战:React 生态与工具 - React 单元测试
前端·javascript·react.js·typescript·单元测试·前端框架
Forworder3 天前
【测试】Bug和用例
selenium·测试工具·单元测试·测试用例·bug·postman·ab测试
多多*3 天前
中间件redis 功能篇 过期淘汰策略和内存淘汰策略 力扣例题实现LRU
linux·javascript·数据库·redis·sql·log4j·bootstrap
绝尘小浪3 天前
用豆包写单元测试
单元测试
枷锁—sha4 天前
【HW系列】—Log4j2、Fastjson漏洞流量特征
安全·web安全·log4j
sunny052964 天前
sl4j+log4j日志框架
java·log4j
菠萝吹雪ing4 天前
测试用例:质量保障的核心利器
功能测试·程序人生·单元测试·测试用例·学习方法·测试覆盖率·安全性测试
编程乐学(Arfan开发工程师)4 天前
25、web场景-【源码分析】-静态资源原理
java·开发语言·前端·spring boot·后端·log4j