mockito学习
mockito学习
写一个测试用例,如果在测试类上面添加了注解@RunWith(SpringJUnit4ClassRunner.class),必须添加@ContextConfiguration("/meta/springConfigured.xml")
否则执行测试用例会报错:Caused by: java.lang.IllegalArgumentException: Cannot load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration or @ContextHierarchy.
package mockitotest; import java.util.Arrays;
import java.util.List; import org.junit.Test;
import org.mockito.ArgumentMatcher; import static org.mockito.Mockito.*;
import static org.mockito.Matchers.*; public class ArgumentMatchersTest { @SuppressWarnings("unchecked")
@Test
public void test() {
List mock = mock(List.class);
when(mock.addAll(argThat(new IsListOfTwoElements()))).thenReturn(true);
mock.addAll(Arrays.asList("one", "two"));
verify(mock).addAll(argThat(new IsListOfTwoElements()));
}
} class IsListOfTwoElements extends ArgumentMatcher<List<String>> {
@SuppressWarnings("unchecked")
public boolean matches(Object list) {
return ((List<String>) list).size() == 2;
}
}
package mockitotest; import java.util.List; import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class)
public class MockitoJUnitRunnerAndMockitoAnnotationsTest { @Mock
private List list; @Test
public void shouldDoSomething() {
list.add(100);
verify(list).add(200);
}
}
/**
*
*/
package mockitotest; /**
* @author Administrator
*
*/
import static org.mockito.Mockito.*; import java.util.LinkedList;
import java.util.List; import org.hamcrest.Matcher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //@RunWith(SpringJUnit4ClassRunner.class)
public class MockitoTest { @Test
public void testMockito_01() {
// TODO Auto-generated method stub
List<String> mockedList = mock(List.class);
mockedList.add("one");
mockedList.clear();
verify(mockedList).add("one");
verify(mockedList).size(); } @Test
public void testMockito_02() {
//You can mock concrete classes, not only interfaces
LinkedList mockedList = mock(LinkedList.class);
//stubbing
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());
//following prints "first"
System.out.println(mockedList.get(0));
//following throws runtime exception
System.out.println(mockedList.get(1));
//following prints "null" because get(999) was not stubbed
System.out.println(mockedList.get(999));
//Although it is possible to verify a stubbed invocation, usually it's just redundant
//If your code cares what get(0) returns then something else breaks (often before even verify() gets executed).
//If your code doesn't care what get(0) returns then it should not be stubbed. Not convinced? See here.
verify(mockedList).get(0);
} @Test
public void testMockito_03() {
//You can mock concrete classes, not only interfaces
LinkedList mockedList = mock(LinkedList.class);
//stubbing using built-in anyInt() argument matcher
when(mockedList.get(anyInt())).thenReturn("element");
//stubbing using hamcrest (let's say isValid() returns your own hamcrest matcher):
when(mockedList.contains(argThat(isValid()))).thenReturn(true);
mockedList.contains(5);
verify(mockedList).contains(argThat(isValid()));
//following prints "element"
System.out.println(mockedList.get(999));
//you can also verify using an argument matcher
verify(mockedList).get(anyInt());
} private Matcher isValid(){
class IsNumber extends ArgumentMatcher<Integer> {
public boolean matches(Object number) {
return ((Integer) number)>10;
}
}
return new IsNumber();
} }
pom.xml文件添加如下依赖
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.8</version>
<scope>test</scope>
</dependency>
如何在spring容器无法找到接口对应的实现类,有一种情况是接口的注解没有加,可以在xml配置文件中添加bean定义,比如
<bean id="productService" class="sardine.commodity.solr.service.impl.ProductServiceImpl"/>
@Mock注解的作用和
<bean id="sendMessage" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.som.SendMessage"></constructor-arg>
</bean>
作用是一样的。
mockito学习的更多相关文章
- Mockito学习1
Mockito学习1 junitmaven软件测试框架项目管理 Mockito是一个流行的Mocking框架.它使用起来简单,学习成本很低,而且具有非常简洁的API,测试代码的可读性很高.因此它十分 ...
- Mockito 学习资料
Mockito 学习资料 网址 单元测试指南:Mockito https://blinkfox.github.io/2018/11/15/hou-duan/java/dan-yuan-ce-shi-z ...
- Mockito学习(zz)
junitmaven软件测试框架项目管理 Mockito是一个流行的Mocking框架.它使用起来简单,学习成本很低,而且具有非常简洁的API,测试代码的可读性很高.因此它十分受欢迎,用 户群越来越 ...
- Mockito学习资料
官网:http://mockito.org/ https://dzone.com/refcardz/mockito
- mockito学习笔记
mockito http://mockito.github.io/mockito/docs/current/org/mockito/Mockito.html
- mockito使用
mockito学习资料: http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html http://blog.csdn.net/sdy ...
- 学习Mockito - Mockito对Annotation的支持
学习Mockito - Mockito对Annotation的支持 博客分类: test junit工作 Mockito支持对变量进行注解,例如将mock对象设为测试类的属性,然后通过注解的方式@M ...
- mockito测试入门学习
一.什么是mock测试,什么是mock对象? 先来看看下面这个示例: 从上图可以看出如果我们要对A进行测试,那么就要先把整个依赖树构建出来,也就是BCDE的实例. 一种替代方案就是使用mocks 从图 ...
- 学习hamcrest和mockito时的总结和demo
UT中需要的jar Junit4.1X.jar hamcrest-library-1.x.jar hamcrest-core-l.x.jar mockito-all-1.10.x.jar Junit ...
随机推荐
- ActionResult派生类
类名 抽象类 父类 功能 ContentResult 根据内容的类型和编码,数据内容. EmptyResult 空方法. FileResult abstract 写入文件内容,具体的写入方式在派生类中 ...
- Web Services 介绍
Web Services 介绍 Web Services 是建立可交互操作的分布式应用程序的新平台 ; Web Services 平台是一套标准,它定义了应用程序如何在 Web 上进行交互操作 , 你 ...
- 『Python』 爬取 WooYun 论坛所有漏洞条目的相关信息
每个漏洞条目包含: 乌云ID,漏洞标题,漏洞所属厂商,白帽子,漏洞类型,厂商或平台给的Rank值 主要是做数据分析使用:可以分析某厂商的各类型漏洞的统计:或者对白帽子的能力进行分析..... 数据更新 ...
- 破解简单Mifare射频卡密钥杂记
先要了解一点大学城浴卡用的是什么卡,经查证是Mifare S50卡,也有叫ISO14443A的(协议名).本来想看看大学城浴卡有没有安全漏洞,没想到浴卡公司在安全性上几乎是蔑视我们大学生的智商,虽然被 ...
- UART接口
1.UART UART(Universal Asynchronous Receiver and Transmitter)通用异步收发器(异步串行通信口),是一种通用的数据通信协议,它包括了RS232. ...
- 使用 Spring Boot 快速构建 Spring 框架应用,PropertyPlaceholderConfigurer
Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2002 年发布以来,Spring 框架已经成为企业应用开发领域非常流行的基础框架.有大量的企业应用基于 Spring 框架来开发.S ...
- Niagara解决设备连接应用的软件框架平台技术。
Niagara 是Tridium公司所研发的设计用于解决设备连接应用的软件框架平台技术. Niagara是一种应用框架,或者说是软件框架,特别设计用于应对智能设备所带来的各种挑战,包括设备连接到企业级 ...
- WAD Forwarder版USB Loader的安装和运行
背景知识 我使用Wad Manager来安装WAD版软件,所以需要先在Wii上面把Wad Manager准备好.详情可以参考我写的另外一篇文章<Wad Manager的下载和运行>(链接: ...
- Flask 安装 Ubuntu 14.04
学习文档: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world 中文版学习文档 开源中国版: ...
- MVC路由机制
按照传统,在很多Web框架中(如经典的ASP.JSP.PHP.ASP.NET等之类的框架),URL代表的是磁盘上的物理文件.例如,当看到请求http://example.com/albums/li ...