记录一下自己如何使用单元测试


前言

记录一下自己如何使用单元测试

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

  • 15
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摸鱼的小张同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值