自己是如何使用单元测试

前言

自己是如何使用单元测试

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

相关推荐
软件检测小牛玛2 天前
具备软件功能测试资质的机构哪家更权威?山东软件测评机构 中承信安
功能测试·单元测试·软件测试报告·软件测评机构
闻哥2 天前
从测试坏味道到优雅实践:打造高质量单元测试
java·面试·单元测试·log4j·springboot
Warren982 天前
Pytest Fixture 作用域与接口测试 Token 污染问题实战解析
功能测试·面试·单元测试·集成测试·pytest·postman·模块测试
知行合一。。。2 天前
程序中的log4j、stderr、stdout日志
python·单元测试·log4j
独自破碎E2 天前
Spring Boot测试启动失败:SLF4J日志多实现冲突解决方案
spring boot·后端·log4j
测试秃头怪3 天前
面试大厂就靠这份软件测试八股文了【含答案】
自动化测试·软件测试·python·功能测试·面试·职场和发展·单元测试
测试大圣3 天前
软件测试基础知识总结(超全的)
软件测试·python·功能测试·测试工具·职场和发展·单元测试·测试用例
CodeCraft Studio3 天前
【Parasoft案例分享】在 DO-178C 标准下,如何实现航空嵌入式软件测试自动化
单元测试·自动化·静态分析·代码覆盖率·parasoft·do-178c·软件自动化测试
niaiheni3 天前
Log4j 漏洞深度分析:CVE-2021-44228 原理与本质
web安全·网络安全·log4j