zoukankan      html  css  js  c++  java
  • spring boot 单元测试

    package com.wfl.weberp.activiti.controller;
    
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.web.WebAppConfiguration;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.web.context.WebApplicationContext;
    
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.hamcrest.Matchers;
    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.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.MvcResult;
    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 com.mysql.fabric.xmlrpc.base.Param;
    import com.wfl.weberp.activiti.Application;
    
    import net.minidev.json.JSONObject;
    
    @RunWith(SpringJUnit4ClassRunner.class) // SpringJUnit支持,由此引入Spring-Test框架支持!
    @SpringBootTest(classes = Application.class) // 指定我们SpringBoot工程的Application启动类
    @WebAppConfiguration // 由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration
    public class ModelCreateControllerTest {
    
        @Autowired
        private WebApplicationContext wac; // 注入WebApplicationContext
    
        private MockMvc mockMvc; // 模拟MVC对象
    
        @Before // 初始化工作
        public void setup() {
            this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
        }
        
        //@Test
        public void TestCreate() throws Exception {
            ///models/model_do/create.do
            HashMap map = new HashMap();
            map.put("key", "keytest");
            map.put("name", "nametest");
            String content = JSONObject.toJSONString(map);
            
            MvcResult result = mockMvc
                    .perform(post("/models/model_do/create.do").contentType(MediaType.APPLICATION_JSON).content(content))
                    .andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))// 预期返回值的媒体类型text/plain;charset=UTF-8
                    .andReturn();// 返回执行请求的结果
    
            System.out.println(result.getResponse().getContentAsString());
        }
    
        @Test
        public void TestList() throws Exception {
    
            MvcResult result = mockMvc
                    .perform(post("/models/model_do/list.do").contentType(MediaType.TEXT_HTML))
                    .andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))// 预期返回值的媒体类型text/plain;charset=UTF-8
                    .andReturn();// 返回执行请求的结果
    
            System.out.println(result.getResponse().getContentAsString());
        }
    
        @Test
        public void TestIndex() throws Exception {
    
            // http://localhost:8080/models/model_do/index.do?_=1513683530848
            mockMvc.perform(get("/models/model_do/index.do?_=1513683530848").contentType(MediaType.TEXT_HTML)
                    .accept(MediaType.TEXT_HTML)).andExpect(MockMvcResultMatchers.status().isOk())
                    .andDo(MockMvcResultHandlers.print())
                    .andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("")));
        }
    
    }
  • 相关阅读:
    PHP之Trait详解
    PHP中__call()方法与重载解析
    PHP Closure(闭包)类详解
    PHP 核心特性
    回调函数
    php的各种 I/O流 以及用法
    关于php的buffer(缓冲区)
    php的运行原理、cgi对比fastcgi以及php-cgi和php-fpm之间的联系区别
    低功耗设计入门(一)——低功耗设计目的与功耗的类型
    从CMOS到触发器(一)
  • 原文地址:https://www.cnblogs.com/rigid/p/8067691.html
Copyright © 2011-2022 走看看