mockito

Android Kotlin: integration tests using Dagger 2 and Mockito getting error, “zero interactions with this mock”

試著忘記壹切 提交于 2020-03-04 19:36:08
问题 I am developing an Android application using Kotlin. I am writing integration tests for my application. Now I am having a problem mocking an injected dependency class that is injected using the Dagger 2 test if a method of the mocked object is called. Following is my code. I have the TestAppComponent class with the following code @Singleton @Component(modules = [ TestAppModule::class ]) interface TestAppComponent { fun inject(app: ApplicationController) fun inject(app:

Dynamic return values with Mockito

一笑奈何 提交于 2020-03-03 09:14:40
问题 One typically defines return values for a Mockito mock as follows: MyClass myClass = Mockito.mock(MyClass.class); when(myClass.myMethod()).thenReturn(0, 100, 200, ...); Is there a way to this dynamically by supplying a seed and a function, e.g.: when(mock.myMethod()).thenReturn(seed, previousVal -> previousVal + 100); 回答1: Yes, you can return an org.mockito.stubbing.Answer . class AddingAnswer implements Answer { int current = 0; public Object answer(InvocationOnMock invocation) { int result

Dynamic return values with Mockito

这一生的挚爱 提交于 2020-03-03 09:14:11
问题 One typically defines return values for a Mockito mock as follows: MyClass myClass = Mockito.mock(MyClass.class); when(myClass.myMethod()).thenReturn(0, 100, 200, ...); Is there a way to this dynamically by supplying a seed and a function, e.g.: when(mock.myMethod()).thenReturn(seed, previousVal -> previousVal + 100); 回答1: Yes, you can return an org.mockito.stubbing.Answer . class AddingAnswer implements Answer { int current = 0; public Object answer(InvocationOnMock invocation) { int result

Dynamic return values with Mockito

妖精的绣舞 提交于 2020-03-03 09:14:04
问题 One typically defines return values for a Mockito mock as follows: MyClass myClass = Mockito.mock(MyClass.class); when(myClass.myMethod()).thenReturn(0, 100, 200, ...); Is there a way to this dynamically by supplying a seed and a function, e.g.: when(mock.myMethod()).thenReturn(seed, previousVal -> previousVal + 100); 回答1: Yes, you can return an org.mockito.stubbing.Answer . class AddingAnswer implements Answer { int current = 0; public Object answer(InvocationOnMock invocation) { int result

PowerMockito verify that a static method is never called

懵懂的女人 提交于 2020-03-03 07:03:28
问题 I am writing a JUnit test to verify that a static method ( MyClass.myMethod() ) is never invoked in the method flow. I tried doing something like this: PowerMockito.verifyStatic(Mockito.never()); MyClass.myMethod(Mockito.any()); In doing so I receive an UnfinisedVerificationException. How do I test that MyClass.class has no interactions whatsoever in the method execution? 回答1: UnfinishedVerificationException will occur if the Class is not mocked yet but you are trying to verify the invocation

PowerMock和Mockito来mock

自闭症网瘾萝莉.ら 提交于 2020-03-02 11:23:04
简单介绍如何使用PowerMock和Mockito来mock 1. 构造函数 2. 静态函数 3. 枚举实现的单例 4. 选择参... 时间 2015-06-16 21:15:05 BlogJava-技术区 原文 http://www.blogjava.net/usherlight/archive/2015/06/16/425740.html 主题 Java 本文将简单介绍如何使用PowerMock和Mockito来mock 1. 构造函数 2. 静态函数 3. 枚举实现的单例 4. 选择参数值做为函数的返回值 一点简要说明:Mockito其实已经可以满足大部分的需求,但是它的实现机制是使用cglib来动态创建接口的类的实例。但是这种实现方式不能用于构造函数和静态函数,因为那需要使用类的字节码(比如使用javassist). 所以我们才需要结合使用PowerMock. 1. mock构造函数, 如果有代码没有使用DI注入依赖实例,在单元测试中可以使用PowerMock来模拟创建对象。 注意的开始两行的2个注解 @RunWith 和 @PrepareForTest @RunWith比较简单,后面始终是PowerMockRunner.class @PrepareForText后面需要加的是调用构造函数的类名,而不是有构造函数的类本身。 在下面的例子中,我们要测试的类是:Helper,

Mockito(一)——入门篇

醉酒当歌 提交于 2020-03-02 09:06:35
Mockito是一种mock工具/框架。我理解EasyMock有点过时了,Mockito是现在比较流行的。 什么是mock?说的直白一点,大家都知道unit test应该是尽可能独立的。对一个class的unit test不应该再和其他class有任何交互。 现在有一个类,扫描一个目录并将找到的文件都上传到FTP server。该类对于不同的FTP响应(找不到FTP server 或 上传成功,或上传失败),有一些后续操作。 在写这个类的UT时,我们就必须虚构出来一个FTP对象。这样在UT中,这个虚构的对象能够代替真正的FTP,对被测试类的调用做出一定的响应。从而知道被测试类是否正确的调用了FTP并做出一些正确的期望的响应。从而达到测试的目的。 mock可以模拟各种各样的对象,从而代替真正的对象做出希望的响应。 关于mock的概念和EasyMock,可以参考: Mock object and EasyMock framework http://blog.csdn.net/OnlyQi/archive/2011/04/26/6364885.aspx 官网: http://mockito.org/ 一篇很好的入门文章: http://blog.csdn.net/huoshuxiao/archive/2010/12/30/6107835.aspx 一些稍微复杂且实用一点的例子: http

Mockito(三)--完整功能介绍

泄露秘密 提交于 2020-03-02 08:38:08
回到官网:http://mockito.org/,打开documentation可以看到原文。 强烈建议不熟悉Mockito的同学先看看我写的Mockito(一)入门篇和(二)实例篇之后再来看这篇文章。 因为只有看了前两篇文章才明白mockito的本质以及该如何使用它。 下面是按原文 翻译+注释 的对Mockito全部功能的介绍。 1, 使用mockito验证行为。 //首先要importMockito. import static org.mockito.Mockito.*; //mock creation List mockedList = mock(List.class); //using mock object mockedList.add("one"); mockedList.clear(); //验证add方法是否在前面被调用了一次,且参数为“one”。clear方法同样。 verify(mockedList).add("one"); verify(mockedList).clear(); //下面的验证会失败。因为没有调用过add("two")。 verify(mockedList).add("two"); 原文中的一句话很重要: Once created, mock will remember all interactions.

Mocking a http response using vert.x rxjava2 and Mockito

江枫思渺然 提交于 2020-02-28 23:18:31
问题 I'm playing with the rxjava vert.x (ver. 3.8.4) WebClient. My client will call some services so, by Mockito (ver. 3.2.4) I'm testing how it should handle an error. Here is how I mock the service's response: ... JsonObject jsonResponse = new JsonObject(); HttpResponse<Buffer> httpResponse = Mockito.mock(HttpResponse.class); Mockito.when(httpResponse.statusCode()).thenReturn(500); Mockito.when(httpResponse.bodyAsJsonObject()).thenReturn(jsonResponse); HttpRequest<Buffer> httpRequest = Mockito

Mocking a http response using vert.x rxjava2 and Mockito

余生长醉 提交于 2020-02-28 23:18:18
问题 I'm playing with the rxjava vert.x (ver. 3.8.4) WebClient. My client will call some services so, by Mockito (ver. 3.2.4) I'm testing how it should handle an error. Here is how I mock the service's response: ... JsonObject jsonResponse = new JsonObject(); HttpResponse<Buffer> httpResponse = Mockito.mock(HttpResponse.class); Mockito.when(httpResponse.statusCode()).thenReturn(500); Mockito.when(httpResponse.bodyAsJsonObject()).thenReturn(jsonResponse); HttpRequest<Buffer> httpRequest = Mockito