网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringBoot单元测试之Mock方式

一、主程序

西峡ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

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());
    }
}

名称栏目:SpringBoot单元测试之Mock方式
文章源于:http://bjjierui.cn/article/gejdps.html

其他资讯