使用ABAP实现Mock测试工具Mockito-阿里云开发者社区

开发者社区> jerrywangsap> 正文

使用ABAP实现Mock测试工具Mockito

简介: 使用ABAP实现Mock测试工具Mockito
+关注继续查看

What is Mockito?


Mockito is a mocking framework, JAVA-based library that is used for effective unit testing of JAVA applications. In our unit test there are usually some dependency on other external service implementation for example network or database service. Usually in order to isolate our test code from these dependencies we have to create mock class against them.


Mockito in Java can create transient mock class ( which is only available in current test session ) for us in a very easy way. See one example below:


This is a very simple class for book manager. Three books are hard coded. If requested book index is bigger than 3, then message 1 “not exist” returns.


In this blog, I will first introduce how Mockito is used in Java to mock this manager class, then show you how the logic of Mockito could be implemented in ABAP.


image.png


Use Mockito in Java

The magic is located in line 18 ~ 23.

First in line 18 a proxy class is generated based on BookManager which is to be mocked, then line 20~23 we told the Mockito “when the method getBook with specified argument ( 0, 1, 2, 3 ) are called, please return given mocked data “mocked book {index}” accordingly.


image.png


And no doubt we can get the following output in console:

image.png

How does it work in Java

Now let me unveil the mistery for you. In order to make the logic better understood, I split the one line

image.png

into the following equivalence:


image.png


The mock process actually consists of four steps:


Step1: create a proxy class by inheriting BookManager.class dynamically.

The generated class is a sub class of BookManager created via CGLIB, which I have already demonstrate how to implement it in ABAP in this blog:


Implement CGLIB in ABAP


image.png


Step2: mockedManager.getBook(0):

Since we call getBook on the mocked class, the original implementation of this method can never be called again. When you click F5 to debug into this method,

you can find the method call of getBook is intercepted into Mockito framework code:


image.png


Inside handler.handle(invocation) in line 47, first an instance of OngoingStubbingImpl is created for later usage.


In line 93, Mockito will check whether there is available mocked data ( In Mockito the terminology for “mocked data” is “answer” ) for current metod call in method findAnswerFor. If available, go to IF branch in line 97 to return mocked data, or else return default answer, in my example I don’t prepare any mocked data for getBook, so it simply returns null.


image.png


The logic of this step could be described in the following activity diagram:


image.png


Step3: Object stubbing = when(“dummy, any string here is ok”);


OngoingStubbingImpl stub = (OngoingStubbingImpl)stubbing;

The static method call simply returns the OngoingStubbingImpl instance created in step2. We need call corresponding method of this instance to feed mocked data.


Step4: stub.thenReturn(“Mocked book!!!”);

This line feeds the mocked data “Mocked book!!!” for method getBook.

The answer is inserted to a ConcurrentLinkedQueue.


image.png


Now the mocked data is ready for consuming in line


System.out.println(mockedManager.getBook(0));

1

Since all the previous four steps for mock are done, we expect this time, the findAnswerfor will really return something.


image.png


Inside this method, each element in the queue is looped to evaluate whether it matches the mocked method call:


image.png


The evaluation is done based on three conditions: mocked class instance must be equal, method name and argument must be equal.


image.png


finally mocked data is returned for getBook in this IF branch.


image.png


How to simulate Mockito in ABAP

image.png

And this is the report to test:

image.png

Execution result:


image.png


Still I will explain how the mock is done in ABAP via the same four steps as in Java Mockito.


Step1: Create a transient sub class dynamically via ABAP call:


zcl_abap_mockito=>mock


The implementation has already been explained in this blog: Implement CGLIB in ABAP


Step2:

lo_mock->get_book( 1 )

When the get_book is executed, of course the original implementation will never be called again. Instead, the mocked class will delegate this call to Mockito framework method zcl_abap_mockito=>get_mocked_data


image.png


In zcl_abap_mockito=>get_mocked_data, just the same as Java, there is a IF branch:


image.png


Step3: call method when to return the instance which is the counterpart for “OngoingStubbingImpl” in Java.


Step4: call method then_return( ‘Data 1’ ) to feed mocked data for get_book method.


Further reading

I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:


Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP

Functional programming – Simulate Curry in ABAP

Functional Programming – Try Reduce in JavaScript and in ABAP

Simulate Mockito in ABAP

A simulation of Java Spring dependency injection annotation @Inject in ABAP

Singleton bypass – ABAP and Java

Weak reference in ABAP and Java

Fibonacci Sequence in ES5, ES6 and ABAP

Java byte code and ABAP Load

How to write a correct program rejected by compiler: Exception handling in Java and in ABAP

An small example to learn Garbage collection in Java and in ABAP

String Template in ABAP, ES6, Angular and React

Try to access static private attribute via ABAP RTTI and Java Reflection

Local class in ABAP, Java and JavaScript

Integer in ABAP, Java and JavaScript

Covariance in Java and simulation in ABAP

Various Proxy Design Pattern implementation variants in Java and ABAP


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
9496 0
MySQL percona-toolkit工具包的安装和使用(超详细版)
转载:https://www.cnblogs.com/zishengY/p/6852280.html 一.检查和安装与Perl相关的模块     PT工具是使用Perl语言编写和执行的,所以需要系统中有Perl环境。
1600 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,大概有三种登录方式:
2927 0
淘系自研前端环境管理工具 AppToolkit 正式发布
AppToolkit提供可视化配置环节的能力,屏蔽环节配置的复杂度和命令行的而学习成本,帮助开发者简单快速搭建前端开发环境。
56 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
10843 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
13186 0
Bytom Kit开发辅助工具介绍
Bytom Kit是一款为了帮助开发者更简单地理解Bytom的开发辅助工具,集合了校验、标注、解码、测试水龙头等功能。 该工具用python语言封装了一套比原的API和7个工具方法,如果有开发需求可以在项目的readme.md文件中查看使用方法。
736 0
使用 mock 数据在本地运行 SAP Fiori Elements 应用的工作原理
使用 mock 数据在本地运行 SAP Fiori Elements 应用的工作原理
12 0
SAP UI5 WebIDE里使用Mock数据的工作原理介绍
Created by Wang, Jerry, last modified on Feb 02, 2015
5 0
+关注
2628
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载