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 ...
随机推荐
- VS2013安装过程截图
================================================================ VS 2013 中新增了很多提高开发人员工作效率的新功能,比如自动补全 ...
- 禁用ios7 手势滑动返回功能
禁用ios7 手势滑动返回功能 版权声明:本文为博主原创文章,未经博主允许不得转载. 在有的时候,我们不需要手势返回功能,那么可以在页面中添加以下代码: - (void)viewDidAppear:( ...
- poi简单案例
//poi api 操作 public static void main(String[] args) { // TODO Auto-generated method stub // 创建一个工作 ...
- 信号量 sem_t 进程同步
sem_t分为有名和无名.有名的sem_t通过sem_open来创建, 而无名的sem_t通过sem_init的初始化. 用有名的sem_t来进程间同步是件很容易的事情,百度上一搜很多想相关的例子. ...
- sql server 国内外 2个同步 ,加一个表.加入同步种
国内 和国外sql server 订阅 ,数据同步. 因为表是刚开始就弄好的. 那么如果国内加一个表.国外没法同步过去 步骤:1.国外也建一个一抹一样的表 步骤:2.把国内的数据导入到国外 步骤:3. ...
- 把图片生成Base64字符串
public class ImgeUtils { public static String img2String(BufferedImage img,String type){ String imgS ...
- H.264 Quantization
H.264 Quantizer 一般的量化器,可用下面的公式来表示: $Z=\pm \left \lfloor\frac{ \left | W \right | }{\bigtriangleup }\ ...
- 多线程爬虫Java调用wget下载文件,独立线程读取输出缓冲区
写了个抓取appstore的,要抓取大量的app,本来是用httpclient,但是效果不理想,于是直接调用wget下载,但是由于标准输出.错误输出的原因会导致卡住,另外wget也会莫名的卡住. 所以 ...
- Java---常用基础面试知识点
综合网上的一点资源,给大家整理了一些Java常用的基础面试知识点,希望能帮助到刚开始学习或正在学习的学员. 1.抽象 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方 ...
- winPcap_5_打开适配器并捕获数据包
知道如何获取适配器的信息了,那我们就开始一项更具意义的工作,打开适配器并捕获数据包.编写一个程序,将每一个通过适配器的数据包打印出来. 打开设备的函数是 pcap_open(). (Open a ge ...