天天看点

使用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.

使用ABAP实现Mock测试工具Mockito

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.

使用ABAP实现Mock测试工具Mockito

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

使用ABAP实现Mock测试工具Mockito

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

使用ABAP实现Mock测试工具Mockito

into the following equivalence:

使用ABAP实现Mock测试工具Mockito

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

使用ABAP实现Mock测试工具Mockito

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:

使用ABAP实现Mock测试工具Mockito

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.

使用ABAP实现Mock测试工具Mockito

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

使用ABAP实现Mock测试工具Mockito

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.

使用ABAP实现Mock测试工具Mockito

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.

使用ABAP实现Mock测试工具Mockito

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

使用ABAP实现Mock测试工具Mockito

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

使用ABAP实现Mock测试工具Mockito

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

使用ABAP实现Mock测试工具Mockito

How to simulate Mockito in ABAP

使用ABAP实现Mock测试工具Mockito

And this is the report to test:

使用ABAP实现Mock测试工具Mockito

Execution result:

使用ABAP实现Mock测试工具Mockito

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

使用ABAP实现Mock测试工具Mockito

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

使用ABAP实现Mock测试工具Mockito

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