前言
老实我以前也没写过单元测试(小公司没要求),都是postman自测OK就提交了
这里第一次写,还废了点时间
做个笔记下次直接抄吧
jar包版本号
<!-- 测试用 start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.5.6</version>
</dependency>
<!-- 测试用 end -->
单元测试代码
package com.mea.pay.transaction;
import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mea.pay.api.dto.transaction.PaymentRecordPayerRemarkUpdateDTO;
import com.mea.pay.transaction.controller.PaymentRecordController;
import com.mea.pay.transaction.dos.PaymentRecordDO;
import com.mea.pay.transaction.service.PaymentRecordService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.math.BigDecimal;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**
* 支付 单元测试
*
* @author Heng.Wei
* @date 2022-05-05
**/
@ActiveProfiles("dev")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class PaymentRecordControllerTest {
/** 单元测试 - 支付方用户ID */
private static final Long UNIT_TEST_PAYMENT_USER_ID = 1L;
/** 单元测试 - 查询用的请求ID */
private static final String UNIT_TEST_PAYMENT_RECORD_REQUEST_ID = "UNIT_TEST_1";
/** 单元测试 - 查询用的主键ID */
private static Long UNIT_TEST_PAYMENT_RECORD_ID;
@Autowired
private PaymentRecordService paymentRecordService;
private MockMvc mockMvc;
@BeforeEach
void setUp() {
mockMvc = standaloneSetup(new PaymentRecordController(paymentRecordService))
.defaultRequest(get("/").accept(MediaType.APPLICATION_JSON))
.alwaysExpect(status().isOk())
.alwaysExpect(content().contentType("application/json"))
.build();
// 初始化测试数据
initTestData();
}
/** 初始化测试数据 */
void initTestData() {
PaymentRecordDO recordDO = paymentRecordService.getOne(new LambdaQueryWrapper<PaymentRecordDO>() {{
eq(PaymentRecordDO::getRequestId, UNIT_TEST_PAYMENT_RECORD_REQUEST_ID);
}});
if (recordDO == null) {
recordDO = new PaymentRecordDO();
recordDO.setRequestId(UNIT_TEST_PAYMENT_RECORD_REQUEST_ID)
.setAmount(new BigDecimal(100))
.setCurrency("USD")
.setPlatformId(1L)
.setTransChannel(1)
.setInitiator(UNIT_TEST_PAYMENT_USER_ID)
.setPayerPan("UNIT_TEST_PAN");
paymentRecordService.save(recordDO);
}
UNIT_TEST_PAYMENT_RECORD_ID = recordDO.getId();
}
/**
* <pre>
* /payment/request/{requestId}
* 根据请求唯一ID获取支付记录
* </pre>
**/
@Test
void getPaymentRecordByRequestId() throws Exception {
mockMvc.perform(get("/payment/request/" + UNIT_TEST_PAYMENT_RECORD_REQUEST_ID)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}
/**
* <pre>
* /payment/{id}
* 根据主键ID查询支付记录详情
* </pre>
**/
@Test
void getPaymentRecordById() throws Exception {
mockMvc.perform(get("/payment/" + UNIT_TEST_PAYMENT_RECORD_ID)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}
/**
* <pre>
* 修改支付记录的用户备注信息
* /payment
* </pre>
**/
@Test
void updatePaymenRecordPayerRemark() throws Exception {
PaymentRecordPayerRemarkUpdateDTO updateDTO = new PaymentRecordPayerRemarkUpdateDTO();
updateDTO.setId(UNIT_TEST_PAYMENT_RECORD_ID);
updateDTO.setPayerRemark("unit test:" + DateTime.now());
updateDTO.setPicurl("unit test");
updateDTO.setTags("unit test");
mockMvc.perform(put("/payment")
.accept(MediaType.ALL_VALUE)
.contentType(MediaType.APPLICATION_JSON)
.content(JSON.toJSONString(updateDTO))
)
.andDo(print())
.andExpect(status().isOk());
}
/**
* <pre>
* /list/{userId}
* </pre>
**/
@Test
void listPaymentRecords() throws Exception {
mockMvc.perform(get("/payment/list/" + UNIT_TEST_PAYMENT_USER_ID)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}
}
测试结果
理论上单元测试是要求可重复验证才对,这里是OK的