SpringBoot单元测试之Mock方式

一、主程序

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

猜你喜欢

转载自blog.51cto.com/11147669/2425227