跟着mall商城学习
mall商城是一个SpringBoot的开源项目,既有mall商城的完整功能实现项目mall
,也有逐步指导的教程项目mall-learning
:
boot开源项目
boot
是我跟着mall写的小项目,目的是绕开复杂的业务逻辑,只考虑最基础的技术实践,掌握SpringBoot框架的使用。计划会包含内容:SpringBoot+MyBatis;Swagger;Redis;JWT认证;定时任务;ElasticSearch;Mongodb;RabbitMQ;OSS文件上传;AOP切面等。
第一波分享内容
项目搭建:
用户增删改查:
登录:
附带JUnit5测试代码
项目附带了JUnit5测试代码:
在写测试代码过程中也体会到了良好约定带来的收益,比如:更新用户接口的测试代码,会通过@BeforeEach
在测试前调用新增接口,准备好数据,拿到用户id,存入变量池vars中:
package com.example.boot.user;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
public class UpdateTest {
private static final Logger LOGGER = LoggerFactory.getLogger(UpdateTest.class);
Map vars = new HashMap<>();
@BeforeEach
public void create() {
LOGGER.info("新增");
String username = RandomStringUtils.randomAlphabetic(6);
vars.put("username", username);
String url = "http://localhost:8070/api/users";
RestTemplate restTemplate = new RestTemplate();
HashMap body = new HashMap<>();
body.put("username", username);
body.put("password", "qa123456");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity> httpEntity = new HttpEntity<>(body, headers);
ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
System.out.println(responseEntity.getBody());
LOGGER.info("获取id");
JSONObject bodyJson = JSONObject.parseObject(responseEntity.getBody());
Long id = bodyJson.getLong("id");
vars.put("id", id);
System.out.println(id);
}
@Test
public void update() {
LOGGER.info("更新");
String url = String.format("http://localhost:8070/api/users/%d", vars.get("id"));
RestTemplate restTemplate = new RestTemplate();
HashMap body = new HashMap<>();
body.put("username", vars.get("username") + "_new");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity> httpEntity = new HttpEntity<>(body, headers);
ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.PUT, httpEntity, String.class);
System.out.println(responseEntity.getBody());
JSONObject bodyJson = JSONObject.parseObject(responseEntity.getBody());
Long id = bodyJson.getLong("id");
assert id != null;
}
}
然后在@Test
测试方法中,从变量池vars
中提取出id,传入url中调用更新接口。
欢迎微信:cekaigang,探讨交流测试经验,加入交流群,相互学习,共同进步。
项目源码:https://github.com/dongfanger/boot