[springboot] 搭建項目及單元測試


本文純屬記錄自己學習的過程以及相關使用操作,使用工具Idea2018。

 

1.創建項目:

 

-- 目錄結構

2.配置文件

 

3.pom文件

4.創建測試類並啟動項目

package com.zr.demo.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Hujh
 * @Date: 2019/7/16 9:32
 * @Description: 項目測試控制器
 */
@RestController
@RequestMapping("test")
public class TestController {

    /**
     * @Title: get
     * @Author : Hujh
     * @Date: 2019/7/16 10:10
     * @Description : 測試方法
     * @param :
     * @Return : java.lang.String
     */
    @RequestMapping("get")
    public String get(){
        return "測試返回數據";
    }

    /**
     * @Title: getByParams
     * @Author : Hujh
     * @Date: 2019/7/16 10:11
     * @Description : 測試帶參方法
     * @param : id
     * @param : name
     * @Return : java.lang.String
     */
    @RequestMapping("getByParams")
    public String getByParams(@RequestParam Integer id, String name){
        StringBuilder sb = new StringBuilder();
        sb.append(id);
        sb.append("---");
        sb.append(name);
        return sb.toString();
    }
}

5.訪問接口,查看返回結果

 

 

進行單元測試:(使用 mockmvc 進行)

 

1.創建測試類

package com.zr.demo;

import com.zr.demo.Controller.TestController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.match.MockRestRequestMatchers;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

/**
 * @Author: Hujh
 * @Date: 2019/7/16 9:38
 * @Description:
 */
@RunWith(SpringRunner.class)
@org.springframework.boot.test.context.SpringBootTest
public class SpringBootTest {

    private Logger logger = LoggerFactory.getLogger(SpringBootTest.class);

    private MockMvc mockMvc;

    @Autowired
    private TestController testController;

    @Before
    public void setUp(){
        logger.info("------初始化執行");
        mockMvc = MockMvcBuilders.standaloneSetup(testController).build();
    }

    /**
     * @Title: test
     * @Author : Hujh
     * @Date: 2019/7/16 9:46
     * @Description : 測試方法
     * @param :
     * @Return : void
     */
    @Test
    public void test() throws Exception {
        MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/test/getByParams")
                .accept(MediaType.APPLICATION_JSON,MediaType.APPLICATION_JSON_UTF8)//請求數據格式
                .contentType(MediaType.APPLICATION_JSON)//接受所用數據格式
                .param("id","123")
                .param("name","小張三");

        // 執行請求
        ResultActions result = mockMvc.perform(requestBuilder);

        //結果解析
        result.andExpect(MockMvcResultMatchers.status().isOk()) // 執行狀態
//                .andExpect(MockMvcResultMatchers.jsonPath("name").value("張三")) //期望值
                .andDo(MockMvcResultHandlers.print()) // 打印
                .andReturn(); // 返回
    }

}

 

 2.啟動test方法,查看返回結果

 

 

總結

使用 Spring Boot 可以非常方便、快速搭建項目,使我們不用關心框架之間的兼容性,適用版本等各種問題,我們想使用任何東西,僅僅添加一個配置就可以,所以使用 Spring Boot 非常適合構建微服務。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM