SpringBoot Junit Demo

Posted songxiaotong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot Junit Demo相关的知识,希望对你有一定的参考价值。

  1 package com.yunen.camera.test;
  2 
  3 import org.junit.Before;
  4 import org.junit.Test;
  5 import org.junit.runner.RunWith;
  6 import org.springframework.beans.factory.annotation.Autowired;
  7 import org.springframework.boot.test.context.SpringBootTest;
  8 import org.springframework.http.MediaType;
  9 import org.springframework.mock.web.MockHttpSession;
 10 import org.springframework.test.annotation.Rollback;
 11 import org.springframework.test.context.junit4.SpringRunner;
 12 import org.springframework.test.web.servlet.MockMvc;
 13 import org.springframework.test.web.servlet.MvcResult;
 14 import org.springframework.test.web.servlet.ResultActions;
 15 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
 16 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
 17 import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
 18 import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
 19 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 20 import org.springframework.web.context.WebApplicationContext;
 21 
 22 import com.YunenCameraManagerApplication;
 23 import com.alibaba.fastjson.JSONObject;
 24 import com.alibaba.fastjson.serializer.SerializerFeature;
 25 
 26 import lombok.extern.slf4j.Slf4j;
 27 
 28 /**
 29  * 相机预置位测试类
 30  * 
 31  * @version V1.0
 32  * @author songxiaotong
 33  * @date 2018年9月17日 下午2:08:14
 34  * @Description
 35  */
 36 @Slf4j
 37 @Rollback(false)
 38 // 让 JUnit 运行 Spring 的测试环境, 获得 Spring 环境的上下文的支持
 39 @RunWith(SpringRunner.class)
 40 // 获取启动类,加载配置,确定装载 Spring 程序的装载方法,它回去寻找 主配置启动类(被 @SpringBootApplication 注解的)
 41 @SpringBootTest(classes = YunenCameraManagerApplication.class)
 42 public class CameraPreSetControllerTest {
 43     /**
 44      * WebApplicationContext
 45      */
 46     @Autowired
 47     private WebApplicationContext wac;
 48 
 49     private MockMvc mockMvc;
 50 
 51     /**
 52      * 初始化mvc请求对象
 53      * 
 54      * @throws Exception
 55      */
 56     @Before
 57     public void setUp() throws Exception {
 58         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
 59     }
 60 
 61     @Test
 62     public void create() {
 63         try {
 64             MockHttpSession session = new MockHttpSession();
 65 
 66             JSONObject param = new JSONObject();
 67             param.put("cameraId", "110");
 68             param.put("name", "预置位名称");
 69             param.put("zoom", 1.1);
 70             param.put("pan", 2.2);
 71             param.put("tilt", 3.3);
 72 
 73             MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
 74                     .post("/yunen/camera_pre_set/create");
 75             requestBuilder.contentType(MediaType.APPLICATION_JSON);
 76 
 77             requestBuilder.content(JSONObject.toJSONString(param,
 78                     SerializerFeature.WriteMapNullValue));
 79             // requestBuilder.content(param.toString());
 80 
 81             requestBuilder.session(session);
 82 
 83             // 执行一个请求;
 84             ResultActions result = mockMvc.perform(requestBuilder);
 85 
 86             // 添加执行完成后的断言
 87             result.andExpect(MockMvcResultMatchers.status().isOk());
 88 
 89             // 添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
 90             result.andDo(MockMvcResultHandlers.print());
 91 
 92             // 表示执行完成后返回相应的结果。
 93             MvcResult mvcResult = result.andReturn();
 94 
 95             log.info(mvcResult.getResponse().getContentAsString());
 96         } catch (Exception e) {
 97             log.error(e.getMessage());
 98         }
 99     }
100 }

 

以上是关于SpringBoot Junit Demo的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot控制层实现单元测试

关于SpringBoot的单元测试

Java RabbitMQ配置和使用,基于SpringBoot

java中springboot集成junit编写单元测试(实战+坑)

springboot-web进阶——单元测试

SpringBoot 集成JUnit