引入依赖的包:

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>

MockMvc + PowerMock + Mockito 来模拟post get请求,并mock掉service中存在doGet doPost外部系统的restful请求

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@prepareForTest({
HttpUtil.class // doGet doPost 使用util类,这里需要告诉UT
})
@PowerMockIgnore("javax.crypto.*")
@AutoConfigureMockMvc
@SpringBootTest
public class DemoSzlTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc; @Before
public void setUp() throws Exceptioin {
MockitoAnnotations.initMocks(this); // 初始化装载powerMockito
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
} @Test
@DirtiesContext
public void testPostDemo() throws Exception {
// mock HttpResponse 对象返回
StringEntity se = new StringEntity("xxxxxx", "utf-8");
HttpResponse resp = new BasicHttpResponse(new BasicStatusLine(HttpVersion.Http_1_1, HttpServletResponse.SC_OK, ""));
resp.setEntity(se);
PowerMockito.mockStatic(HttpUtil.class);
// 这里的 Mockito.anyString() 要和 实际方法参数保持一致
PowerMockite.when(HttpUtil.doGet(Mockito.anyString(), Mockito.anyMap(), Mockito.any())).thenReturn(resp);
// 这里不 mock方法,直接走真实场景方法
PowerMockito.when(HttpUtil.toJson(Mockito.anyObject())).thenCallRealMethod();
// body contents
String jsonBody = "{\"aaaa\":\"1111\"}";
String token = "xxxxx";
MvcResult resultMsg = mockMvc.perform(
post("/xxx")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
.header("xxxxx", token)
.content(jsonBody) // request body
).andExpect(status().isCeated())
.andReturn();
}     @Test
    @DirtiesContext
    public void contextLoads() throws Exception {
        // 这里定义的 header 值 必须要和service层使用的值保持一致,否则powerMock mock时会有问题
        // 其实也很好理解,既然要mock,那就做到真实一点,一切都保持一模一样即可。
        Header[] header = {
                new BasicHeader("Authorization", "aaabbbccc"),
                new BasicHeader("Content-Type", "application/json")
            };
        
        // 手动创建HttpResponse,来模式http请求的返回值
        StringEntity se = new StringEntity("{"
                + "\"memo\":\"这个只是demo式样\"}", "utf-8");
        HttpResponse resp = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1,
                HttpServletResponse.SC_OK, ""));
        resp.setEntity(se);
        
        // 和上面说明一致
        StringEntity se2 = new StringEntity("{"
                + "\"memo\":\"这个只是demo2式样\"}", "utf-8");
        HttpResponse resp2 = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1,
                HttpServletResponse.SC_OK, ""));
        resp2.setEntity(se2);
        
        // 开始mock我们想要的静态方法,此处是mock post请求并返回上面我们mock的值
        PowerMockito.mockStatic(HttpUtil.class);
        /**
         * 本人是为了达到一个service里连续调用2次post请求,但2次请求内容不同,返回结果也不同,进行mock
         * Mockito.eq("/aaa")               是mock值
         * Mockito.eq("aaabbb")             是mock值
         * Mockito.any(header.getClass())   是mock Header[]值
         */
        PowerMockito.when(HttpUtil.doPost(Mockito.eq("/aaa"), Mockito.eq("aaabbb"), Mockito.any(header.getClass()))).thenReturn(resp);
        PowerMockito.when(HttpUtil.doPost(Mockito.eq("/bbb"), Mockito.eq("cccddd"), Mockito.any(header.getClass()))).thenReturn(resp2);
        
        String jsonBody = "{\"aaaa\":\"1111\"}";
        String token = "xxxxx";
        MvcResult resultMsg = mockMvc.perform(
                 post("/testUTDemo")
                 .header("Authorization", token)
                 .content(jsonBody)    // request body
        ).andExpect(status().isCreated()).andReturn();
    } }

Controller类:

package com.szl.demo.szldemo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.szl.demo.szldemo.common.dto.UserDto;
import com.szl.demo.szldemo.service.UserService;
import lombok.extern.slf4j.Slf4j; @Slf4j
@Controller
public class DemoController {
@Autowired
private UserService userService; @ResponseBody
@RequestMapping(value = "/testUTDemo", method = RequestMethod.POST)
public UserDto testUTDemo(HttpServletRequest request, HttpServletResponse response) {
return userService.testDemoUnitTest();
} }

Service接口:

package com.szl.demo.szldemo.service;

import com.szl.demo.szldemo.common.dto.UserDto;

public interface UserService {

    UserDto testDemoUnitTest();

}

Service实现类:

package com.szl.demo.szldemo.service.impl;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.message.BasicHeader;
import org.springframework.stereotype.Service;
import com.szl.demo.szldemo.common.dto.UserDto;
import com.szl.demo.szldemo.common.util.HttpUtil;
import com.szl.demo.szldemo.service.UserService;
import lombok.extern.slf4j.Slf4j; @Slf4j
@Service("userService")
public class UserServiceImpl implements UserService { public UserDto testDemoUnitTest() {
try {
Header[] header = {
new BasicHeader("Authorization", "aaabbbccc"),
new BasicHeader("Content-Type", "application/json")
};

// 此处的代码块也是mock的重点
HttpResponse resp = HttpUtil.doPost("/aaa", "aaabbb", header);
HttpEntity entity = resp.getEntity();
String msg = IOUtils.toString(entity.getContent(), "UTF-8");
log.info("msg: " + msg);

// 同上
HttpResponse resp2 = HttpUtil.doPost("/bbb", "cccddd", header);
HttpEntity entity2 = resp2.getEntity();
String msg2 = IOUtils.toString(entity2.getContent(), "UTF-8");
log.info("msg2: " + msg2);
} catch (Exception e) {
e.printStackTrace();
}

// 这里随便赋值并返回,主要不是看这里的功能
UserDto dto = new UserDto();
dto.setId(1001L);
dto.setNickName("test");
dto.setUserId(10000001L);
return dto;
} }

HttpUtil.java类:

public class HttpUtil {
public static HttpResponse doGet(String url, Map<String, String> params, Header[] headers) {
.....
这里省略
} public static JsonObject toJson(HttpResponse httpResponse) {
.....
这里省略
}
public static HttpResponse doPost(String url, String params, Header[] header) {
        // 这里只是为了演示,手动创建StringEntity并赋值,手动创建HttpResponse并返回
        HttpResponse resp = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null));
        StringEntity se1 = new StringEntity("{"
                + "\"alg\":\"aqas\","
                + "\"id\":\"122011\"}", "utf-8");
        resp.setEntity(se1);
        return resp;
}
}

UserDto类:

package com.szl.demo.szldemo.common.dto;

import java.io.Serializable;
import lombok.Data; @Data
public class UserDto implements Serializable {
private static final long serialVersionUID = -8858511759866491158L; private Long id;
private Long userId;
private String nickName; }

好了,让咋们来试试上面的UT跑出什么结果,如下图:

OK, 大功告成,如有博友需要,供参考,需转发,请注明引自链接,谢谢。

另附上额外功能,如果有朋友需要在UT里按顺序执行,需要加注解:

@FixMethodOrder(xxx)

这里的xxx有三种模式:

- 默认(MethodSorters.DEFAULT)
- 按方法名(MethodSorters.NAME_ASCENDING)
- JVM(MethodSorters.JVM)

一开始本人使用JVM模式,虽然能达到预期,但运行不太稳定,本人还是推荐大家使用NAME_ASCENDING模式

如有朋友参考本人的笔记,有问题可以留言,转载请注明原著,谢谢。

springboot2.1.3+Junit4 单元测试的更多相关文章

  1. SpringBoot使用Junit4单元测试

    SpringBoot2.0笔记 本篇介绍Springboot单元测试的一些基本操作,有人说一个合格的程序员必须熟练使用单元测试,接下来我们一起在Springboot项目中整合Junit4单元测试. 本 ...

  2. JUnit4 单元测试

    一. 题目简介 这次的单元测试我作了一个基本运算的程序,该程序实现了加,减,乘,除,平方,倒数的运算,该程序进行测试比较的简单,对于初步接触JUnit的我来说测试起来也比较容易理解. 二.源码的git ...

  3. Junit4单元测试

    在Eclipse中使用JUnit4进行单元测试(初级篇) 在Eclipse中使用JUnit4进行单元测试(中级篇) 在Eclipse中使用JUnit4进行单元测试(高级篇)

  4. JUnit4单元测试基础篇

    引言 JUnit作为Java语言的测试框架,在测试驱动开发(TDD)下扮演重要的角色.众所周知,无论开发大型项目还是一般的小型项目, 单元测试都至关重要.单元测试为软件可发测试维护提供了很大的便利.J ...

  5. Spring 下使用Junit4 单元测试

    package platform; import java.util.List; import java.util.UUID; import javax.annotation.Resource; im ...

  6. Junit4单元测试的基本用法

    看了一些Junit4的视频,简单了解了Junit4的一些基本用法,整理记录一下. 环境搭建 这里使用的开发工具是MyEclipse,首先新建一个Java工程,将Junit4的jar包引入,eclips ...

  7. Method 'initializationerror' not found.Opening the test classs JUnit4单元测试报错问题解决办法(图文详解)

    不多说,直接上干货! 问题现象 今天使用JUnit 4进行单元测试时,测试程序一直运行不起来,报method initializationerror not found错误,如下: 问题分析 网上说版 ...

  8. JUnit4单元测试入门教程

    本文按以下顺序讲解JUnit4的使用 下载jar包 单元测试初体验 自动生成测试类 执行顺序 @Test的属性 下载jar包## 下载地址 在github上,把以下两个jar包都下载下来.     下 ...

  9. Java Web(5) Spring 下使用Junit4 单元测试

    1. 如何在不启动Tomcat服务器的情况下对,Dao这些不依赖使用Servlet API的类来进行单元测试呢? 其实在Spring框架体系中,已经有一套自己的测试代码,其中就是依赖使用Junit来进 ...

随机推荐

  1. Oracle系列十二 约束

    约束是表级的强制规定有以下五种约束: NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK 注意事项 如果不指定约束名 ,Oracle server 自动按照 S ...

  2. Python之内置装饰器property

    # -*- coding: utf-8 -*- # author:baoshan class Student(object): def __init__(self, name): self.name ...

  3. [转]彻底解决deepin linux的无线网络问题

    链接地址:https://bbs.deepin.org/forum.php?mod=viewthread&tid=153154

  4. [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  5. git删除远程.idea目录

    git删除远程.idea目录 1. 登录 Administrator@USER-20180708AB MINGW32 / (master) $ git config --global user.nam ...

  6. VS2019/VS2017设置默认管理员权限启动

    找到vs安装目录下的:C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenv.exe 右键- ...

  7. 【视频开发】ffmpeg实现dxva2硬件加速

    这几天在做dxva2硬件加速,找不到什么资料,翻译了一下微软的两篇相关文档.这是第二篇,记录用ffmpeg实现dxva2. 第一篇翻译的Direct3D device manager,链接:http: ...

  8. ztree取消之前选中状态并设置新的选中

    var zTreeObject = $.fn.zTree.getZTreeObj("treeId"); var node = zTreeObject.getNodeByParam( ...

  9. day45——html常用标签、head内常用标签

    day45 MySQL内容回顾 数据库 DBMS mysql -RDBMS 关系型 数据库分类 关系型:mysql\oracle\sqlserver\access 非关系型:redis,mongodb ...

  10. PAT(B) 1060 爱丁顿数(Java:21分)

    题目链接:1060 爱丁顿数 (25 point(s)) 题目描述 英国天文学家爱丁顿很喜欢骑车.据说他为了炫耀自己的骑车功力,还定义了一个"爱丁顿数" E ,即满足有 E 天骑车 ...