TDD单元测试类方法模板

说明

1.此模板使用的请求方式为http,大家可以更改为mockmvc的这种方式
2.登录方式也比较笨重,如果自己公司有封装好的单元测试专用注解方式登录方式更好哦

TDD使用模板

package com.cpmc.cpmcbidding.project.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpmc.cpmcbidding.project.App;
import com.cpmc.cpmcbidding.project.query.BidInvitationAgencyQuery;
import com.cpmc.cpmcbidding.project.util.UserAuthUtil;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Map;

/**
 * @author wuzhenyong
 * ClassName:TemplateTest.java
 * date:2021-12-14 17:20
 * Description:
 */
@SpringBootTest(classes = {App.class})
public class TemplateTest {
	@Value("${security.encode.key}")
	public String encodeKey;
	@Value("${gateway.address}")
	public String gatewayAddress;
    public String token;

    @BeforeEach
    void setUp() {
    	// 登录
		Map<String, Object> headMap = UserAuthUtil.login("admin", "wzgs@123456QQ",encodeKey, gatewayAddress);
		token = MapUtil.getStr(headMap, "access_token");
    }

    @Test
    public void get() {
		Object object = new Object();
		Page page = new Page(1,10);
		Map<String, Object> queueMap = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class, true);
		Map<String, Object> pageMap = JSONUtil.toBean(JSONUtil.toJsonStr(page), Map.class, true);
		queueMap.putAll(pageMap);
		String body = HttpRequest.get("接口地址")
				.form(queueMap)
				.header("Authorization", token)
				.execute().body();
		JSONObject jsonObject = JSONUtil.parseObj(body);
		String code = jsonObject.getStr("code");
		Assert.assertEquals("预期结果", code);
    }
	@Test
	public void post() {
		Object object = new Object();
		Map<String, Object> map = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class, true);
		String body = HttpRequest.post("接口地址")
				.form(map)
				.header("Authorization", token)
				.execute().body();
		JSONObject jsonObject = JSONUtil.parseObj(body);
		String code = jsonObject.getStr("code");
		Assert.assertEquals("预期结果", code);
	}
	@Test
	public void put() {
		Object object = new Object();
		Map<String, Object> map = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class, true);
		String body = HttpRequest.put("接口地址")
				.form(map)
				.header("Authorization", token)
				.execute().body();
		JSONObject jsonObject = JSONUtil.parseObj(body);
		String code = jsonObject.getStr("code");
		Assert.assertEquals("预期结果", code);
	}
	@Test
	public void delete() {
		Object object = new Object();
		Map<String, Object> map = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class, true);
		String body = HttpRequest.delete("接口地址")
				.form(map)
				.header("Authorization", token)
				.execute().body();
		JSONObject jsonObject = JSONUtil.parseObj(body);
		String code = jsonObject.getStr("code");
		Assert.assertEquals("预期结果", code);
	}
}

posted @ 2022-05-06 16:46  勇不停歇  阅读(31)  评论(0)    收藏  举报