前言
老实我以前也没写过单元测试(小公司没要求),都是postman自测OK就提交了 这里第一次写,还废了点时间 做个笔记下次直接抄吧
jar包版本号
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.5.6</version>
</dependency>
单元测试代码
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.*;
@ActiveProfiles("dev")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class PaymentRecordControllerTest {
private static final Long UNIT_TEST_PAYMENT_USER_ID = 1L;
private static final String UNIT_TEST_PAYMENT_RECORD_REQUEST_ID = "UNIT_TEST_1";
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();
}
@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());
}
@Test
void getPaymentRecordById() throws Exception {
mockMvc.perform(get("/payment/" + UNIT_TEST_PAYMENT_RECORD_ID)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}
@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());
}
@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的
|