Mockito测试 注解
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
@RunWith(MockitoJUnitRunner.class)
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource("test.properties")
@ContextConfiguration(classes = {A.class, B.class})
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
xxx-test.properties
单个文件
@ContextConfiguration(Locations="../applicationContext.xml")
@ContextConfiguration(classes = SimpleConfiguration.class)
多个文件时,可用{}
@ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" })
@ContextConfiguration(classes = {TestG10TempestContext.class, RatesTopologyContext.class})
@EnableOAuth2Client
@Configuration
@PropertySource("test.properties")
public class TestTempestContext {
@Value("${xx.id}")
private String id;
@Bean
public String str() {
return "123";
}
}
ReflectionTestUtils.setField(str, "name", "123");
@EnableOAuth2Client
package hello.hello_spock; import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate; @RunWith(MockitoJUnitRunner.class)
public class RestTemplateTest { @SuppressWarnings("unchecked")
@Test
public void testRestTemplate() {
RestTemplate restTemplate = mock(RestTemplate.class);
when(restTemplate.execute(anyString(),
any(HttpMethod.class), any(RequestCallback.class), any(ResponseExtractor.class)))
.thenReturn("123"); String aString=restTemplate.execute("http://www.baidu.com", HttpMethod.GET, x->System.out.println("a"), x-> "" );
assertEquals(aString, "123");
} }
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>hello</groupId>
<artifactId>hello_spock</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>hello_spock</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies> </project>
Mockito测试 注解的更多相关文章
- 用Mockito测试SpringMVC+Hibernate
用Mockito测试SpringMVC+Hibernate 译自:Spring 4 MVC+Hibernate 4+MySQL+Maven integration + Testing example ...
- 强大的Mockito测试框架(转)
1.自动生成Mock类在需要Mock的属性上标记@Mock注解,然后@RunWith中配置Mockito的TestRunner或者在setUp()方法中显示调用MockitoAnnotations.i ...
- 使用强大的 Mockito 测试框架来测试你的代码
原文链接 : Unit tests with Mockito - Tutorial 译文出自 : 掘金翻译计划 译者 : edvardhua 校对者: hackerkevin, futureshine ...
- 强大的Mockito测试框架
转载:https://blog.csdn.net/dc_726/article/details/8568537 1自动生成Mock类 在需要Mock的属性上标记@Mock注解,然后@RunWith ...
- Junit mockito 测试Controller层方法有Pageable异常
1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...
- Mockito测试
Mockito 一 mockito基本概念 Mock测试是单元测试的重要方法之一,而Mockito作为一个流行的Mock框架,简单易学,且有非常简洁的API,测试代码的可读性很高. Mock测试就是在 ...
- mockito测试入门学习
一.什么是mock测试,什么是mock对象? 先来看看下面这个示例: 从上图可以看出如果我们要对A进行测试,那么就要先把整个依赖树构建出来,也就是BCDE的实例. 一种替代方案就是使用mocks 从图 ...
- 用maven搭建 testNG+PowerMock+Mockito测试框架
单元测试是开发中必不可少的一部分,是产品代码的重要保证. Junit和testNG是当前最流行的测试框架,Junit是使用最广泛的测试框架,有兴趣的话自己baidu一下. testNG基于Junit和 ...
- TestNG多线程测试-注解方式实现
用@Test(invocationCount = x,threadPoolSize = y)声明,invocationCount表示执行次数,threadPoolSize表示线程池大小. packag ...
随机推荐
- ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...
- QT(4)信号与槽
mainWidget.h #ifndef MAINWIDGET_H #define MAINWIDGET_H #include <QWidget> #include <QPushBu ...
- 图形化升级单机oracle 11.2.0.4 到 12.2.0.1
1. 讲补丁包上传到 Oracle server ,解压.安装 [oracle@11g tmp]$ unzip linuxx64_12201_database.zip 2. 检查当前版本 SQL> ...
- oracle隐含参数的查看与修改
v$parameter视图中查询参数的时候其实都是通过x$ksppi和x$ksppcv这两个内部视图中得到的. 1. 可以通过如下方式查询当前实例的所有隐含参数: col name for a30 ...
- 数据结构-二叉搜索树的js实现
一.树的相关概念 1.基本概念 子树 一个子树由一个节点和它的后代构成. 节点的度 节点所拥有的子树的个数. 树的度 树中各节点度的最大值 节点的深度 节点的深度等于祖先节点的数量 树的高度 树的高度 ...
- Python:更改字典的key
思路:先删除原键值对,保存值,然后以新键插入字典 格式:dict[newkey] = dict.pop(key) d = {'a':1, 'aa':11} d['b'] = d.pop('a') d[ ...
- 二、java 与 scala相互调用
介绍:scala 是简化的java,运行于jvm的脚步语言.Java和scala通过各自编译器编译过都是jvm能解析class文件.本文介绍java和scala如何互调 scala的源代码文件是以. ...
- hdu 1506 单调栈问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目的意思其实就是要找到一个尽可能大的矩形来完全覆盖这个矩形下的所有柱子,只能覆盖柱子,不能留空 ...
- sql 基础总结
- C#设计模式(11)——外观模式
一.概念 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用.使用外观模式时,我们创建了一个统一的类,用来包装子系统中一个或多个复杂的类,客户端可以直 ...