junit 是一个面向于研发人员使用的轻量的测试模块,适合做单元测试。而testng百度后发现更强大,可以做功能测试,但对于我这种RD,貌似junit足沟了呢!

java Mock PowerMock、jmockit 模拟包非常优秀,我目前选型学会一个就不错了,不做评价,我学的powermock,据说jmockit更强大。但是powermock貌似继承了旧的easymock和jmock,所以用的人比较大(个人理解)。

对于junit测试的几个注解

@RunWith

@Before

@Test

@After

powerMock注解

@Mock

@InjectMocks

@RunWith 表示运行方式,@RunWith(JUnit4TestRunner)、@RunWith(SpringRunner.class),@RunWith(PowerMockRunner.class) 三种运行方式,分别在不同的场景中使用。

@Before是 @Test运行之前调用的方法,可以做初始化操作

@Test  是测试用例的单元

@After 执行完测试用例需要执行的清理工作

@Mock  有点类似Autowired注解,而@Mock注解是自动实现模拟对象,而并非Bean实例创建

@InjectMocks 实现对@Mock注解的对象注入到当前对象中,通过MockitoAnnotations.initMocks(this);实现

package aa;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import static org.mockito.Matchers.any;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(PowerMockRunner.class)
@SpringBootTest(classes = MainApplication.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore("javax.management.*")
public class moreTest { @Mock
TService tService;
@InjectMocks
TController controller;
@Autowired
private WebApplicationContext ctx;
private MockMvc mvc; @Before
public void before() throws Exception { MockitoAnnotations.initMocks(this);
//如果简单mock一个请求可以使用这个,但是要模拟注入,还是按下面的方式实现controller比较好
// mvc = MockMvcBuilders.webAppContextSetup(ctx).build();
mvc = MockMvcBuilders.standaloneSetup(controller).build();
// taskPackageService=PowerMockito.mock(TService);
PowerMockito.when(tService.queryMethod(any(ParameClass.class)))
.thenReturn(88);
PowerMockito.whenNew(TService.class).withNoArguments().thenReturn(tService);
} @Test
public void searchTest() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/welcome").header("User-Agent", "xx")
.param("page", "1")
).andExpect(status().isOk()).andDo((MvcResult result) -> {
System.out.println(result.getResponse().getContentAsString());
});
} }

spring boot 测试用例的更多相关文章

  1. Spring Boot 知识图谱

    最近有意重新学习下SpringBoot知识,特地总结了SpringBoot的知识点,对于想要学习的人来说,够用. SpringBoot学习路径 第一部分:了解 Spring Boot Spring B ...

  2. Spring Boot的一个测试用例

    package tk.mybatis.springboot.mapper; import org.junit.Assert; import org.junit.Test; import org.jun ...

  3. Spring Boot中的事务管理

    原文  http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...

  4. Spring Boot 乐观锁加锁失败 - 使用AOP恢复错误

    之前写了一些辅助工作相关的Spring Boot怎么使用AOP.这里继续正题,怎么减少Spring Boot 乐观锁加锁报错的情况(基本可以解决). 1. 包依赖 spring-boot-starte ...

  5. Spring Boot文档阅读

    原因之初 最初习惯百度各种博客教程,然后跟着操作,因为觉得跟着别人走过的路走可以少走很多弯路,省时间.然而,很多博客的内容并不够完整,甚至错误,看多了的博客甚至有千篇一律的感觉.此外,博客毕竟是记载博 ...

  6. 用Gradle构建Spring Boot项目

    相比起Maven的XML配置方式,Gradle提供了一套简明的DSL用于构建Java项目,使我们就像编写程序一样编写项目构建脚本.本文将从无到有创建一个用Gradle构建的Spring Boot项目, ...

  7. Spring Boot 学习笔记

    参考资料 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ Spring Boot简介 Spring Boot使 ...

  8. springboot(十一):Spring boot中mongodb的使用

    mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...

  9. Spring Boot快速入门(三):依赖注入

    原文地址:https://lierabbit.cn/articles/6 spring boot使用依赖注入的方式很简单,只需要给添加相应的注解即可 @Service用于标注业务层组件 @Contro ...

随机推荐

  1. Matplotlib风羽自定义

    [前言]对于气象专业的小学生来说,风场是预报重要的参考数据,我们所知的风羽有四种:短线代表风速2m/s,长线代表风速4m/s,空心三角代表风速20m/s,实心三角代表风速50m/s.而matplotl ...

  2. hbase 多个过滤器组合(列表)

    使用FilterList要保证过滤器的顺序需要使用List<Filter> private static void mutilFilterData() throws IOException ...

  3. Android Paging库使用详解

    Android分页包能够更轻易地在RecyclerView里面缓慢且优雅地加载数据. 许多应用从数据源消耗数据, 数据源里面有大量的数据, 但是一次却只展示一小部分. 分页包帮助应用观测和展示大量数据 ...

  4. 【AGC010F】Tree Game

    Description 有一棵\(n\)个节点的树(\(n \le 3000\)),第\(i\)条边连接\(a_i,b_i\),每个节点\(i\)上有\(A_i\)个石子,高桥君和青木君将在树上玩游戏 ...

  5. WebSphere概要文件管理工具

    manageprofiles:概要管理命令工具 3.1 模板 位于 <was_home>/profileTemplates.每个模板都由一组提供概要初始设置的文件和概要创建后将采取的操作列 ...

  6. java的序列化流和打印流

    对象操作流(序列化流) 每次读取和写出的都是JavaBean对象. 序列化:将对象写入到文件中的过程 反序列化:从文件中读取对象到程序的过程 transient: 标识瞬态,序列化的时候,该修饰符修饰 ...

  7. ---web模型 --mvc和模型--struts2 入门

    关于web模型: 早期的web 应用主要是静态页丽的浏览〈如新闻的制监),随着Internet的发展,web应用也变得越来越复杂,不仅要 和数据库进行交互 ,还要和用户进行交互,由此衍生了各种服务器端 ...

  8. spark 调优——基础篇

    开发调优 调优概述 Spark性能优化的第一步,就是要在开发Spark作业的过程中注意和应用一些性能优化的基本原则.开发调优,就是要让大家了解以下一些Spark基本开发原则,包括:RDD lineag ...

  9. bzoj 3190 赛车 半平面交

    直接写的裸的半平面交,已经有点背不过模板了... 这题卡精度,要用long double ,esp设1e-20... #include<iostream> #include<cstd ...

  10. c/c++基本数据类型转换

    If either operand is of type long double, the other operand is converted to type long double. If the ...