package com.icil.esolution.orders;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List; import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
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.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.icil.esolution.pojo.InventoryQueryVO;
import com.icil.esolution.pojo.SOOrderVO;
import com.icil.esolution.pojo.TOA;
import com.icil.esolution.utils.JsonUtils;
import com.icil.esolution.utils.URLHttpUtils;
/**
*
* @ClassName: OrderControllerTest
* @Description:
* @Author: Sea
* @Date: 12 july 2018 5:02:06 PM
* @Copyright: 2018 ICIL All rights reserved.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderControllerTest {
@Autowired
private WebApplicationContext wac; private MockMvc mockMvc; @Before
public void Setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
} @Test
public void TestQuerySOOrder() throws Exception {//customerCode, String warehouseCode
String result = mockMvc.perform(
get("/order/querySOOrder")
.param("customer", "PDX000055")
.param("warehouseCode", "ULHKG")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(); System.out.println(result);
} @Test
public void testUploadExcel() throws Exception { // File file = new File("/home/sea/Desktop/esolution/E+Outbound_Template.xls");
// byte[] readFileToByteArray = FileUtils.readFileToByteArray(file);
// String result = mockMvc.perform(fileUpload("/order/uploadExcel")
// .file(new MockMultipartFile("ePlusExcel", "test.xls", "multipart/form-data", readFileToByteArray))
// .param("warehouseCode", "ULHKG")
// .param("customer","PDX000031"))
// .andExpect(status().isOk())
// .andReturn().getResponse().getContentAsString();
// System.out.println(result); File srcFile=new File("/home/sea/Desktop/esolution/Outbound_Template_eplus.xls");
String originalFileName = srcFile.getName();
InputStream is = new FileInputStream(srcFile); //Upload excel template and return Order TO for confirmation
MockMultipartFile uploadFile = new MockMultipartFile("ePlusExcel",originalFileName , null, is); MvcResult andReturn = mockMvc.perform(fileUpload("/order/uploadExcel").file(uploadFile).param("warehouseCode", "ULHKG").param("customer","PDX000031").accept("application/json;charset=UTF-8"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andReturn();
System.out.println(andReturn); } /**
* POST方式 传带文件的调用
* @return
* @throws Exception
*/
@Test
public void PostMethodFileTest() throws Exception{
System.out.println("开始");
String targetUrl="http://localhost:8080/order/uploadExcel";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(targetUrl);
try{
File srcFile=new File("/home/sea/Desktop/esolution/E+Outbound_Template.xls"); InputStream ePlusExcels = new FileInputStream(srcFile); FilePart filePart = new FilePart("ePlusExcel",srcFile);//文件参数
//new FilePart("ss", (PartSource) ePlusExcels); // StringPart ePlusExcel=new StringPart("ePlusExcel", ePlusExcels.toString());
StringPart customer= new StringPart("customer", "PDX000031");
StringPart warehouseCode=new StringPart("warehouseCode", "ULHKG");
// method.setParameter("customer", "PDX000031");
// method.setParameter("warehouseCode", "ULHKG");
// StringPart questionId = new StringPart("questionId","10001");//普通参数
// StringPart userId = new StringPart("userId","765709");//普通参数
// StringPart homeworkId = new StringPart("homeworkId","950");//普通参数 Part[] parts ={filePart,customer,warehouseCode};
MultipartRequestEntity mre=new MultipartRequestEntity(parts ,method.getParams()); //封装了普通字段和文件字段
method.setRequestEntity(mre);
int result = client.executeMethod(method);
if (result == HttpStatus.SC_OK) {
InputStream in = method.getResponseBodyAsStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[];
int len = ;
while ((len = in.read(buffer)) != -) {
baos.write(buffer, , len);
}
String response = URLDecoder.decode(baos.toString(), "UTF-8");
System.out.println("response*****"+response);
} else {
throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
}
}finally {
method.releaseConnection();
}
} @Test
public void testquerySOOrderDetail() throws Exception {//customerCode, String warehouseCode
String result = mockMvc.perform(
get("/order/querySOOrderDetail")
.param("orderNo", "SO-HKG-00152542")
.param("warehouseCode", "ULHKG")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(); System.out.println(result);
} @Test
public void testQueryInventory() throws UnsupportedEncodingException, Exception{
String result = mockMvc.perform(get("/order/queryInventory")
.param("warehouseCode", "ULHKG")
.param("customerCode", "PDX000031")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println(result); } @Test
public void testName() throws Exception {
HashMap<String, Object> hashMap = new HashMap<>(); hashMap.put("aa", "ss");
String objectToJson = JsonUtils.objectToJson(hashMap);
System.out.println(objectToJson);
} @Test
public void TestapiQueryOrderDetails() throws Exception { Header header = URLHttpUtils.getBasicAuthorizationHeader("testmsg1", "");
String response = URLHttpUtils.getURLRequest("https://192.168.18.176/wosedi/ws/apiQueryOrderDetails?orderNo=SO-HK-00152542&warehouseCode=ULHKG", header);
String exception = MDC.get("concectException");
System.err.println("exception is ******************:"+exception);
System.out.println(response); } @Test
public void testJsontoList() throws Exception {
File file = new File("src/test/java/resource/SoOrderVo.json");
String data = FileUtils.readFileToString(file);
System.err.println(data);
//Object json = JSON.toJSON(data);
// SoOrderVO soOrderVO = JSON.parseObject(data, SoOrderVO.class); //List<SoOrderVO> jsonToList = JsonUtils.jsonToList(data, SoOrderVO.class);
Gson gson = new Gson();
//SoOrderVO fromJson = gson.fromJson(data, SoOrderVO.class); //List<SoOrderVO> jsonToList =gson.fromJson(data, new TypeToken<List<SoOrderVO>>() {}.getType());
List<SOOrderVO> jsonToList = JsonUtils.gsonTOList(data, SOOrderVO.class);
System.out.println("result is :"+jsonToList); } @Test
public void testQueryOrderAll() throws Exception {
File file = new File("src/test/java/resource/QueryOrderALL.json");
String data = FileUtils.readFileToString(file);
System.err.println(data);
//Object json = JSON.toJSON(data);
// SoOrderVO soOrderVO = JSON.parseObject(data, SoOrderVO.class); //List<SoOrderVO> jsonToList = JsonUtils.jsonToList(data, SoOrderVO.class);
Gson gson = new Gson();
//SoOrderVO fromJson = gson.fromJson(data, SoOrderVO.class); List<SOOrderVO> jsonToList =gson.fromJson(data, new TypeToken<List<SOOrderVO>>() {}.getType());
System.out.println("this is first data"+jsonToList.get()); //List<SoOrderVO> jsonToList = JsonUtils.gsonTOList(data, SoOrderVO.class);
System.out.println("result is :"+jsonToList); } @Test
public void testGsontoList() throws Exception {
File file = new File("src/test/java/resource/inventoryQueryResponseData.json");
String data = FileUtils.readFileToString(file);
System.err.println(data);
//Object json = JSON.toJSON(data);
// SoOrderVO soOrderVO = JSON.parseObject(data, SoOrderVO.class);
//List<SoOrderVO> jsonToList = JsonUtils.jsonToList(data, SoOrderVO.class);
Gson gson = new Gson();
//SoOrderVO fromJson = gson.fromJson(data, SoOrderVO.class);
// List<InventoryQueryVO> jsonToList =gson.fromJson(data, new TypeToken<List<InventoryQueryVO>>() {}.getType());
List<InventoryQueryVO> jsonToList = JsonUtils.gsonTOList(data, InventoryQueryVO.class);
System.out.println("result is :"+jsonToList);
} @Test
public void testWOS4TOA() throws Exception {
File file = new File("src/test/java/resource/Wos4ResponseData.json");
String data = FileUtils.readFileToString(file,"UTF-8");
System.out.println(data);
Object json = JSON.toJSON(data);
TOA toa = JSON.parseObject(data, TOA.class); System.out.println(toa.getResult());
System.out.println(toa.getMesgs());
System.out.println(toa.getData()); } @Test
public void testJDK8DataFormart() throws Exception { DateTimeFormatter dateTimeFormater = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss");
LocalDateTime localDateTime = LocalDateTime.now();
Date date = new Date();
System.out.println("【----new date----】" + date);
System.out.println("【----未格式化之前----】" + localDateTime);
System.out.println("【----格式化之后----】"+dateTimeFormater.format(localDateTime)); } }

MOCK 基本使用例子的更多相关文章

  1. 说说初用 Mock 工具测试碰到的坑

    我是一个在校实习生,作为一个程序猿,是个菜鸟中战斗机!对于测试,只写过一点点简单到不能再简单了的 Junit 单元测试的例子(因为当时这足以应付学校课程的内容与要求).这几天在公司里要真枪实弹做测试的 ...

  2. 【Python】解决测试依赖之 Mock模块的基本使用

    什么是mock? Mock,顾名思义,模拟,在我们日常生活中或者影视作品中见得最多的可能就是预备飞行员的模拟训练,印象比较深的是电影<萨利机长>中的模拟器,经过几千次模拟,人们得出机长萨利 ...

  3. 单元测试-mock基础

    本文较短,只是备份一下mock的几个常用基础例子方便复习 目录 介绍mock的使用例子 maven资源 <dependency> <groupId>org.mockito< ...

  4. google mock C++单元测试框架

    转:google mock C++单元测试框架 2012-03-12 09:33:59 http://blog.chinaunix.net/uid-25748718-id-3129590.html G ...

  5. 使用Mockito进行单元测试【2】—— stub 和 高级特性[转]

    一篇中介绍了Mockito的基本信息,现在接着介绍Mockito强大的stub功能 2. Mockito使用实例 5. 对连续的调用进行不同的返回 (iterator-style stubbing) ...

  6. 前端模拟后台返回数据之Mockjs

    一.官方文档: https://github.com/nuysoft/Mock/wiki/Syntax-Specification 例子:http://mockjs.com/examples.html ...

  7. Mockito图书馆

    转载:https://static.javadoc.io/org.mockito/mockito-core/2.12.0/org/mockito/Mockito.html#42 org.mockito ...

  8. 使用Powermock和mockito来进行单元测试

    转载:http://blog.csdn.net/u013428664/article/details/44095889 简介 Mockito是一个流行的Mocking框架.它使用起来简单,学习成本很低 ...

  9. 学习Mockito - Mockito对Annotation的支持

    学习Mockito - Mockito对Annotation的支持 博客分类: test junit工作  Mockito支持对变量进行注解,例如将mock对象设为测试类的属性,然后通过注解的方式@M ...

随机推荐

  1. java内存的分配和管理

    常用的三个内存空间 栈内存 ,堆内存 ,方法区 栈内存存储的内容: 局部变量. 函数(栈中的局部变量,需要手动赋值.当变量,或者函数执行完毕,就自动被释放) 堆内存,存储的内容 :全局变量.数据容器. ...

  2. wpf 客户端【JDAgent桌面助手】开发详解(三) 瀑布流效果实现与UI虚拟化优化大数据显示

    目录区域: 业余开发的wpf 客户端终于完工了..晒晒截图 wpf 客户端[JDAgent桌面助手]开发详解-开篇 wpf 客户端[JDAgent桌面助手]详解(一)主窗口 圆形菜单... wpf 客 ...

  3. apache flink docker-compose 运行试用

    apache 是一个流处理框架,官方提供了docker 镜像,同时也提供了基于docker-compose 运行的说明 docker-compose file version: "2.1&q ...

  4. 捷报 FastAdmin 国内开源排名第 13 名

    捷报 FastAdmin 国内开源排名第 13 名 FastAdmin 是一款基于 ThinkPHP 5 + Bootstrap 的后台开源框架. 去年是第 35 名. 今年是第 13 名,有进步.

  5. linux挂载SD卡

    (1)通过#fdisk -l命令确认板子上的linux系统是否识别SD卡 MP805M板子插入SD卡后显示 SD30 slot is without WPmmc1: new high speed SD ...

  6. IPv6调用java后端接口报错:java.net.SocketException: Protocol family unavailable

    目前需求是java后端的接口需要支持IPv6.先确认linux机器已经绑定了IPv6: CMREAD-SV43 apache-tomcat/bin> ifconfig eth0 Link enc ...

  7. 【python】格式化字符

    格式化字符串总结如下,红色部分是需要掌握部分: 以下几个常用的实例: 1.%s的使用 "%s is the author" %("paulwinflo")> ...

  8. vmware :Ubuntu 12.04添加新硬盘

    http://blog.csdn.net/hanpengyu/article/details/7475645 一.VMware新增磁盘的设置步骤 (建议:在设置虚拟的时候,不要运行虚拟机的系统,不然添 ...

  9. [模板] KMP字符串匹配标准代码

    之前借鉴了某个模板的代码.我个人认为这份代码写得很好.值得一背. #include<bits/stdc++.h> using namespace std; const int N=1000 ...

  10. idea远程调试jar包

    1 服务器启动jar包 (监听端口5005) java -Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=y -j ...