mockito

Mockito verify that a specific lambda has been passed as an argument in mock's method

旧街凉风 提交于 2020-01-10 20:23:31
问题 I want to test the following method: public void dispatchMessage(MessageHandler handler, String argument1, String argument2, Long argument3) { handler.registerMessage(() -> { dispatcher.dispatch(argument1, argument2, argument3); }); } Where MessageHandler is a helper class which will accept a Functional Interface implementation in the form a lambda, and store it for later execution. Is there a way to verify with mockito that the dispatchMessage method of the mocked MessageHandler has been

Mockito bypass static method for testing

你。 提交于 2020-01-10 02:34:24
问题 I need to test handleIn() method using Mockito. However the code need to call this legacy code Util.getContextPDO which is a static method. Note that in testing environment this Util.getContextPDO is always returns Exception, and I intend to bypass this Util.getContextPDO() by always return a dummy IPDO. public class MyClass { public IPDO getIPDO() { return Util.getContextPDO(); // note that Util.getContextPDO() is a static, not mockable. } public String handleIn(Object input) throws

TestNG + Mockito + PowerMock - verifyStatic() does not work

↘锁芯ラ 提交于 2020-01-09 11:22:29
问题 I am new TestNG and unit-testing in general. I am using TestNG 6.9.6 with Mockito 1.10.19 and PowerMock 1.6.4. I want to verify whether the myMethod() method in MyService class internally calls the static method Util.myStaticMethod with the correct arguments. Since verification of static methods is not natively supported in Mockito, I am using PowerMock along with it. My Test class is shown below: public class MyTest { private MyService myService; @Captor ArgumentCaptor<String> argCaptor;

5分钟了解Mockito

倾然丶 夕夏残阳落幕 提交于 2020-01-08 19:37:26
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 一、什么是mock测试,什么是mock对象? 先来看看下面这个示例: 从上图可以看出如果我们要对A进行测试,那么就要先把整个依赖树构建出来,也就是BCDE的实例。 一种替代方案就是使用mocks 从图中可以清晰的看出 mock对象就是在调试期间用来作为真实对象的替代品。 mock测试就是在测试过程中,对那些不容易构建的对象用一个虚拟对象来代替测试的方法就叫mock测试。 知道什么是mock测试后,那么我们就来认识一下mock框架---Mockito 二、什么是Mockito 除了有一个好记的名字外,Mockito尝试用不一样的方法做mocking测试,是简单轻量级能够替代EasyMock的框架。使用简单,测试代码可读性高,丰富的文档包含在javadoc中,直接在IDE中可查看文档,实例,说明。更多信息: http://code.google.com/p/mockito/ 三、Stub和Mock 相同点:Stub和Mock对象都是用来模拟外部依赖,使我们能控制。 不同点:而stub完全是模拟一个外部依赖,用来提供测试时所需要的测试数据。而mock对象用来判断测试是否能通过,也就是用来验证测试中依赖对象间的交互能否达到预期。在mocking框架中mock对象可以同时作为stub和mock对象使用,两者并没有严格区别

Mockito

泪湿孤枕 提交于 2020-01-08 19:36:04
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 前言 在多个成员协同开发的时候,有没有遇到过类似这样的情况: A成员在开发某个接口但是没有完成,而B恰好依赖于A,在不了解Mockito的情况下只能等待A完成之后才可以自测,完全受制于他人,不知你是否想过怎么摆脱这样一个尴尬的场景,下面咱们就根据这样一个场景来做具体讲述。 目录 什么是Mockito? 为什么需要模拟? 有哪些需要注意的关键点? 如何Mock? 结合Junit使用 注意事项 正文 1. 什么是Mockito? Mockito是一个单元测试框架,封装了简洁易用的API,对于新手来说也可以很容易的入门,同时在我们编写单元测试的时候还可以以锻炼我们的逻辑思维能力。 2. 为什么需要模拟? 原因一: 在我们实际程序开发中经常遇到下图这种类与类之间的层级依赖: 正如前言所说,假设ImageService还未开发完毕,此时我们的开发将受限于他人 原因二: 在我们写单元测试的时候,业务层的数据大多数人都是从数据库读取,假设某位同事把数据不小心删除了,那我们的单元测试就有可能出问题。专业的说法就是:数据与应用隔离 原因三: 锻炼我们的逻辑思维能力,为什么这么说呢?在我们写Mock单元测试的时候,你必须了解你的逻辑,对你的逻辑做一些验证,比如说测试A服务的时候调用B,需要在调用B服务的时候做参数抓取校验

一文全面了解Android单元测试

浪尽此生 提交于 2020-01-08 10:28:04
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 前言 成为一名优秀的Android开发,需要一份完备的 知识体系 ,在这里,让我们一起成长为自己所想的那样~。 ==》完整项目单元测试学习案例 众所周知,一个好的项目需要不断地打造,而一些有效的测试则是加速这一过程的利器。本篇博文将带你了解并逐步深入Android单元测试。 什么是单元测试? 单元测试就是针对类中的某一个方法进行验证是否正确的过程,单元就是指 独立的粒子 ,在Android和Java中大都是指方法。 为什么要进行单元测试? 使用单元测试可以提高开发效率,当项目随着迭代越来越大时,每一次编译、运行、打包、调试需要耗费的时间会随之上升,因此,使用单元测试可以不需这一步骤就可以对单个方法进行功能或逻辑测试。 同时,为了能测试每一个细分功能模块,需要将其相关代码抽成相应的方法封装起来,这也在一定程度上改善了代码的设计。因为是单个方法的测试,所以能更快地定位到bug。 单元测试case需要对这段业务逻辑进行验证。在验证的过程中,开发人员可以 深度了解业务流程 ,同时新人来了看一下项目单元测试就知道 哪个逻辑跑了多少函数,需要注意哪些边界 ——是的,单元测试做的好和文档一样 具备业务指导能力。 Android测试的分类 Android测试主要分为三个方面: 单元测试(Junit4、Mockito

Junit for JiraRestClient create Issue

南笙酒味 提交于 2020-01-07 03:09:10
问题 I am trying to write a JUnit test for following method which creates a new Jira issue, do anyone know how to mock JiraRestClient or is there any other way to write a test for this My code is public Issue createNewIssue(BasicProject project, BasicUser assignee, BasicIssueType issueType, String summary, String description, String parentKey, File attachment) { try { IssueInputBuilder issueBuilder = new IssueInputBuilder(project, issueType); issueBuilder.setDescription(description); issueBuilder

Mockito when()…then() NullPointerException

人盡茶涼 提交于 2020-01-07 03:04:55
问题 productsInDatabse is a hashMap, output is enum, scanCode is from class which is tested public static boolean isInDataBase(int code) { return productsInDatabse.containsKey(code); } and I've got a test: @Test public void testScanCodeForCodeNotFound() { Database db = Mockito.mock(Database.class); when(db.isInDataBase(444)).thenReturn(false); output = scanner.scanCode("444"); assertTrue(output == ProductProcessing.PRODUCT_NOT_FOUND); } But when()...then() returns with NPE. I saw examples when

Create a Junit test case for a method which contains a call to another method from different class which calls to a different method of the same class

烂漫一生 提交于 2020-01-07 02:58:21
问题 I am new to java and Unit testing. I am facing a problem in writing a unit test to one of the test methods. Can you please help me with this. ClassA: Class classA{ /*......*/ classB method1_ClassA(parameters){ /*..... ..... ..... ..... .....*/ String some_Identifier = method2_ClassA(parameters) } private String method2_ClassA(parameters){ /*contains call to the database*/ } } ClassB: Class classB{ /*.....*/ } ClassC: Class classC{ /*.......*/ public classB method1_ClassC(parameters){ classA

使模拟方法返回传递给它的参数

旧城冷巷雨未停 提交于 2020-01-07 02:08:10
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 考虑如下方法签名: public String myFunction(String abc); Mockito可以帮助返回该方法收到的相同字符串吗? #1楼 如果您具有Mockito 1.9.5或更高版本,则有一个新的静态方法可以为您创建 Answer 对象。 你需要写一些像 import static org.mockito.Mockito.when; import static org.mockito.AdditionalAnswers.returnsFirstArg; when(myMock.myFunction(anyString())).then(returnsFirstArg()); 或者 doAnswer(returnsFirstArg()).when(myMock).myFunction(anyString()); 注意, returnsFirstArg() 方法在 AdditionalAnswers 类中是静态的,它是Mockito 1.9.5的新增功能。 因此您需要正确的静态导入。 #2楼 您可以在Mockito中创建答案。 假设我们有一个名为Application的接口,该接口带有方法myFunction。 public interface Application { public