溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot單元測試之Mock方式

發布時間:2020-07-06 07:42:48 來源:網絡 閱讀:3692 作者:戀上程序員 欄目:編程語言

一、主程序

package com.kyy.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 啟動程序
 * @Auther:zhouhongliang
 * @Date:2019/7/30
 * @Description:
 */
@SpringBootApplication
public class SpringBootDemo {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemo.class,args);
    }
}

二、處理器

package com.kyy.springboot.controller;

import com.kyy.springboot.model.Person;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther:zhouhongliang
 * @Date:2019/7/30
 * @Description:
 */
@RestController
public class HelloController {
    /**
     * 無參訪問
     *
     * @return
     */
    @RequestMapping("/hello")
    public String hello() {
        return "hello word";
    }

    /**
     * restful 風格訪問
     *
     * @param name
     * @return
     */
    @RequestMapping("/hello1/{name}")
    public String hello1(@PathVariable(name = "name") String name) {
        return "hello " + name;
    }

    /**
     * get 訪問,傳遞參數
     *
     * @param par1
     * @param par2
     * @return
     */
    @RequestMapping(value = "/hello2", method = RequestMethod.GET)
    public String hello2(String par1, Integer par2) {
        return "hello " + par1 + "," + par2;
    }

    /**
     * post 訪問 傳遞表單數據
     *
     * @param par
     * @return
     */
    @RequestMapping(value = "/hello3", method = RequestMethod.POST)
    public String hello3(String par) {
        return "hello " + par;
    }

    /**
     * post 訪問 參數放到請求體
     *
     * @param par
     * @return
     */
    @RequestMapping(value = "/hello4")
    public String hello4(@RequestBody String par) {
        return "hello " + par;
    }

    @RequestMapping(value = "/hello5")
    @ResponseBody
    public Person hello5(@RequestBody Person person) {
        return person;
    }
}

三、模型

package com.kyy.springboot.model;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @Auther:zhouhongliang
 * @Date:2019/7/31
 * @Description:
 */
public class Person implements Serializable{
    private Integer sid;
    private String name;
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date birthday;
    private BigDecimal salary;
    private double bonus;

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public BigDecimal getSalary() {
        return salary;
    }

    public void setSalary(BigDecimal salary) {
        this.salary = salary;
    }

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }
}

四、Mock方式單元測試

package com.kyy.springboot.controller;

import com.kyy.springboot.SpringBootDemo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
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;
import org.springframework.web.context.WebApplicationContext;

import java.net.URI;

/**
 * @Auther:zhouhongliang
 * @Date:2019/7/31
 * @Description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootDemo.class)
@WebAppConfiguration
public class HelloControllerTest {
    @Autowired
    private WebApplicationContext webApplicationContext;
    private MockMvc mockMvc;

    @Before
    public void setupMockMvc() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void testHello() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get(new URI("/hello")))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print());
    }

    @Test
    public void testHello1() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/hello1/admin"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print());
    }

    @Test
    public void testHello2() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/hello2").param("par1", "admin").param("par2", "100"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print());
    }

    @Test
    public void testHello3() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.post("/hello3").param("par", "admin"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print());
    }

    @Test
    public void testHello4() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.post("/hello4")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content("par"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print());
    }

    @Test
    public void testHello5() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.post("/hello5")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content("{\"sid\":1,\"name\":\"admin\",\"birthday\":\"1983-10-22\",\"salary\":\"1000\",\"bonus\":100}"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print());
    }
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女