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>

最新文章

  1. webpack初入
  2. php字符串笔记
  3. shell+Jenkins+jmeter集成
  4. myeclipse 闪退解决方法
  5. nginx 伪静态大于10个参数 $10
  6. [Sdoi2017]相关分析 [线段树]
  7. MySQL中group_concat函数深入理解
  8. Java开发笔记(七十五)异常的处理:扔出与捕捉
  9. 导出dmp
  10. Task与线程池
  11. 【Web前端】用CSS3实现弹幕
  12. 数据结构c++实现代码-链表
  13. python实战===教你用微信每天给女朋友说晚安
  14. python+ajaxFileUpload 无刷新上传文件
  15. vs code 插件收集
  16. 51nod 1003 阶乘后面0的数量
  17. ORB_SLAM2 源码阅读 ORB_SLAM2::ORBextractor
  18. [java] byte不能直接相加
  19. GPG error: http://extras.ubuntu.com trusty Release: The following signatures couldn&#39;t be verified because the public key is not available: NO_PUBKEY F60F4B3D7FA2AF80
  20. Linux Shell参数替换

热门文章

  1. ACM学习历程—HDU 5451 Best Solver(Fibonacci数列 &amp;&amp; 快速幂)(2015沈阳网赛1002题)
  2. const关键字祥解
  3. 愚人的linux内核2440移植札记(超曲折版)
  4. Ajax的包装
  5. 字符串(String)
  6. 17、GATK使用简介 Part2/2
  7. 获得Servlet的初始化参数
  8. 8.bwapp亲测xxe漏洞
  9. [Linux]关于sigprocmask函数的讨论
  10. return die exit 常用