自己是如何使用单元测试

前言

自己是如何使用单元测试

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

相关推荐
hqxstudying16 小时前
java依赖注入方法
java·spring·log4j·ioc·依赖
新world16 小时前
mybatis-plus从入门到入土(二):单元测试
单元测试·log4j·mybatis
仰望星空@脚踏实地2 天前
Spring Boot Web 服务单元测试设计指南
spring boot·后端·单元测试
啃火龙果的兔子4 天前
前端单元测试覆盖率工具有哪些,分别有什么优缺点
前端·单元测试
编程乐学(Arfan开发工程师)13 天前
75、单元测试-嵌套测试
前端·javascript·redis·python·单元测试·bootstrap
编程乐学(Arfan开发工程师)13 天前
73、单元测试-断言机制
服务器·数据库·servlet·单元测试·sqlite·log4j·mybatis
编程乐学(Arfan开发工程师)13 天前
74、单元测试-前置条件
redis·python·阿里云·单元测试·云计算·bootstrap
不一样的少年_14 天前
前端单元测试的救星:Vitest 输入和 Mock 技术详解
前端·单元测试
十连满潜14 天前
springboot集成mockito和jacoco实践
后端·单元测试·mockito
bo5210017 天前
vue3单元测试-项目实战
vue.js·单元测试