Mocking SAP S/4HANA Calls

After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Explain mocking capabilities of SAP Cloud SDK using MockUtil class

Mocking SAP S/4HANA Calls without an SAP S/4HANA System

In automated unit testing, it is sometimes necessary to use objects or procedures that look and behave like their release-intended counterparts, but are actually simplified versions that reduce complexity and facilitate testing. Such procedures are called test doubles. Mocking refers to the idea of providing these test doubles instead of the real implementation during tests. This helps in reducing the testing effort in cases where testing could be too cumbersome, or it is not possible to instantiate the entire program.

Furthermore, as we discussed with the testing pyramid, doing more unit tests is preferable during the development of the application as it forces application developers to write more testable application code and apply separation of concerns more thoroughly, which usually leads to more maintainable and understandable code. Using Mockito with the SAP Cloud SDK, we can test a small portion of the code without the need for a real SAP S/4HANA back-end system. This provides a very convenient way to test the overall functionality.

Mockito is an open source testing framework for Java released under the MIT License. The framework allows the creation of test double objects (mock objects) in automated unit tests for the purpose of test-driven development (TDD) or behavior-driven development (BDD).

To know more about Mockito, review the official documentation at

Code sample to mock

Consider our existing Business Partner Address Manager application. In the GetAllBusinessPartnersCommand command, the run() method does a type-safe OData call to the BusinessPartner API of the SAP S/4HANA system. This fetches the business partner details and address. To view the code, navigate to your project and choose address-managerapplicationsrcmainjava.

When it comes to mocking, this piece of code is tested locally rather than against a real SAP S/4HANA system. To achieve this, we add a new test command under the unit-tests directory. To access the unit-tests directory, choose address-managerunit-testssrctestjava.

Example of Test Command to Add

The following is an example of the code required to add a new test command called GetBusinessPartnerMockedTest:

Code snippet
package com.example;

import com.example.adman.BusinessPartnerRepository;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.List;



import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

public class GetBusinessPartnerMockedTest
    private static BusinessPartner tom = BusinessPartner.builder().firstName("Tom").build();
    private static BusinessPartner kathy = BusinessPartner.builder().firstName("Kathy").build();

    public void testGetAnyBusinessPartner()
        final BusinessPartnerService service = Mockito.mock(BusinessPartnerService.class, RETURNS_DEEP_STUBS);
        BusinessPartnerRepository.service = service;

                .orderBy(any(), any())
                .withHeader(any(), any())
                .thenReturn(Lists.newArrayList(tom, kathy));

        final List<BusinessPartner> businessPartnerList = BusinessPartnerRepository.findAll();

        assertEquals(2, businessPartnerList.size());
        assertEquals("Tom", businessPartnerList.get(0).getFirstName());
        assertEquals("Kathy", businessPartnerList.get(1).getFirstName());

Log in to track your progress & complete quizzes