温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot单元测试之Mock方式

发布时间:2020-07-06 07:42:48 来源:网络 阅读:3668 作者:恋上程序员 栏目:编程语言

一、主程序

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