1. 程式人生 > 其它 >SpringBoot基礎實踐開源專案分享(一)

SpringBoot基礎實踐開源專案分享(一)

跟著mall商城學習

mall商城是一個SpringBoot的開源專案,既有mall商城的完整功能實現專案mall,也有逐步指導的教程專案mall-learning

boot開源專案

boot是我跟著mall寫的小專案,目的是繞開復雜的業務邏輯,只考慮最基礎的技術實踐,掌握SpringBoot框架的使用。計劃會包含內容:SpringBoot+MyBatis;Swagger;Redis;JWT認證;定時任務;ElasticSearch;Mongodb;RabbitMQ;OSS檔案上傳;AOP切面等。

第一波分享內容

專案搭建:

  • SpringBoot框架

  • MySQL表

  • MyBatis自動生成Mapper和Model

使用者增刪改查:

  • 獲取所有使用者列表、新增使用者、更新指定id使用者資訊、刪除指定id的使用者、驗證授權、獲取指定id的使用者詳情、修改密碼、重置密碼

登入:

  • 登入以後返回token

  • JWT認證

附帶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<String, Object> 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<String, Object> body = new HashMap<>();
        body.put("username", username);
        body.put("password", "qa123456");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<HashMap<String, Object>> httpEntity = new HttpEntity<>(body, headers);
        ResponseEntity<String> 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<String, Object> body = new HashMap<>();
        body.put("username", vars.get("username") + "_new");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<HashMap<String, Object>> httpEntity = new HttpEntity<>(body, headers);
        ResponseEntity<String> 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