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测试 注解的更多相关文章

  1. 用Mockito测试SpringMVC+Hibernate

    用Mockito测试SpringMVC+Hibernate 译自:Spring 4 MVC+Hibernate 4+MySQL+Maven integration + Testing example ...

  2. 强大的Mockito测试框架(转)

    1.自动生成Mock类在需要Mock的属性上标记@Mock注解,然后@RunWith中配置Mockito的TestRunner或者在setUp()方法中显示调用MockitoAnnotations.i ...

  3. 使用强大的 Mockito 测试框架来测试你的代码

    原文链接 : Unit tests with Mockito - Tutorial 译文出自 : 掘金翻译计划 译者 : edvardhua 校对者: hackerkevin, futureshine ...

  4. 强大的Mockito测试框架

    转载:https://blog.csdn.net/dc_726/article/details/8568537 1自动生成Mock类   在需要Mock的属性上标记@Mock注解,然后@RunWith ...

  5. Junit mockito 测试Controller层方法有Pageable异常

    1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...

  6. Mockito测试

    Mockito 一 mockito基本概念 Mock测试是单元测试的重要方法之一,而Mockito作为一个流行的Mock框架,简单易学,且有非常简洁的API,测试代码的可读性很高. Mock测试就是在 ...

  7. mockito测试入门学习

    一.什么是mock测试,什么是mock对象? 先来看看下面这个示例: 从上图可以看出如果我们要对A进行测试,那么就要先把整个依赖树构建出来,也就是BCDE的实例. 一种替代方案就是使用mocks 从图 ...

  8. 用maven搭建 testNG+PowerMock+Mockito测试框架

    单元测试是开发中必不可少的一部分,是产品代码的重要保证. Junit和testNG是当前最流行的测试框架,Junit是使用最广泛的测试框架,有兴趣的话自己baidu一下. testNG基于Junit和 ...

  9. TestNG多线程测试-注解方式实现

    用@Test(invocationCount = x,threadPoolSize = y)声明,invocationCount表示执行次数,threadPoolSize表示线程池大小. packag ...

随机推荐

  1. UNITY_MATRIX_IT_MV[Matrix]

    http://blog.csdn.net/cubesky/article/details/38682975 前面发了一篇关于unity Matrix的文章. http://blog.csdn.NET/ ...

  2. navicat导入sql文件

    Hello,大家好.Navicat是我们平时使用较多的一个数据库客户端工具,平时小天我主要是用来连接mysql的,使用的时候还是很方便的. 今天小天我就给大家分享一个Navicat如何导入导出sql文 ...

  3. HDOJ1166(线段树点修改)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. Python:代码单元、代码点介绍

    转于:https://www.cnblogs.com/runwulingsheng/p/5106078.html 博主:你是那天边突然划过的一道闪电 代码点:指编码表(比如Unicode)中某个字符的 ...

  5. RS485总线防雷保护方案

    RS485作为最为最常用的电表通讯方式之一.日常生活中雷电和静电干扰已经成为485通信总线在实际工程经常遇到的问题.故如何对芯片以及总线进行有效的保护,是摆在每一个使用者面前的一个问题.在这里,我们主 ...

  6. oop的方式来操纵时间

    减少return 减少传参. 主要是在调用上比以前强大很多,以前很怕操作时间,在一堆函数中传来传去.这个调用爽. class DatetimeConverter: DATETIME_FORMATTER ...

  7. shell入门-wc

    命令:wc 选项:-l  查看行数 -w  以空白字符为分隔符 查看有多少单词 -m  查看字符数,文件大小 说明:统计指定文件中的字节数.字数.行数. -l [root@wangshaojun 11 ...

  8. MySQL的limit优化

    mysql的分页比较简单,只需要limit offset,length就可以获取数据了,但是当offset和length比较大的时候,mysql明显性能下降 1.子查询优化法 先找出第一条数据,然后大 ...

  9. [51nod1270] 数组的最大代价(简单dp)

    解题关键:先由贪心的思想得出任何一个位置只能取1或者a[i],然后dp即可. #include<bits/stdc++.h> using namespace std; typedef lo ...

  10. assert.ok()

    测试 value 是否为真值. 相当于 assert.equal(!!value, true, message). 如果 value 不为真值,则抛出一个带有 message 属性的 Assertio ...