SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
⤢ Open full site

Write an ABAP Unit Test for the RAP Business Object

Write an ABAP Unit Test for the RAP business object.

Overview

🎓 beginner 20 min. SAP BTP ABAP EnvironmentBeginnerABAP DevelopmentSAP Business Technology Platform

You will learn

  • How to create ABAP unit test class
  • How to enhance test class definition
  • How to implement special methods
  • How to implement test method
  • How to run your ABAP unit test
Merve Temel M Merve Temel April 12, 2024
Created by March 9, 2023
Contributors

Prerequisites

Prerequisites

  • You need to have access to an SAP BTP, ABAP environment, or SAP S/4HANA Cloud, ABAP environment or SAP S/4HANA (release 2022 or higher) system. For example, you can create free trial user on SAP BTP, ABAP environment.
  • You have downloaded and installed the [latest ABAP Development Tools (ADT)] (https://tools.hana.ondemand.com/#abap) on the latest Eclipse© platform.
  • You have created an ABAP Cloud Project.
  • Make sure, your system has the ABAP flight reference scenario. If your system hasn’t this scenario. You can download it here. The trial systems have the flight scenario included.

Steps

Intro

In the previous exercise, you’ve implemented the dynamic instance feature control for some of the standard and non-standard operations of the Travel entity.

In the present exercise, you will write scenario tests for your business object to verify its transactional behavior as a whole. You will write an ABAP Unit test with the Entity Manipulation Language (EML). The scenario to be tested in the present exercise is the creation and manipulation of a new travel instance.


Reminder: Do not forget to replace the suffix placeholder ### with your chosen or assigned group ID in the exercise steps below.

About: ABAP Unit Testing

Ensuring the high quality of applications has a very high priority in the whole software development lifecycle. As an application developer, you want to be able to write, for example, unit, scenario and integration tests to verify the application behavior as a whole. The ABAP platform offers different mechanisms and frameworks to achieve this. The main options here are ABAP Unit Tests and the ABAP Test Cockpit.

Writing ABAP Unit tests is the way to provide high quality software, which can be easily evolved over time without introducing regressions. In methodologies, like extreme programming and test driven development, the role of unit testing is even more important.

ABAP Unit is the state-of-the-art unit testing framework for ABAP. It’s embedded into the ABAP programming language which supports you in writing unit tests. In ADT you have various possibilities to execute the unit tests and to evaluate the results concerning functional correctness and code coverage.

Further reading : Test@RAP Development Guide | Testing the RAP Business Object | Ensuring Quality of ABAP Code

About: Simplified Runtime flow of ABAP Unit Test Classes

The ABAP Unit framework has standard special methods which needs to be implemented to set up and destroy a given test configuration.

The standard special static methods of the test configuration class_setup() and class_teardown(), class_setup(): This setup method is executed once before all tests of the class. It is used to setup the test environment.

class_teardown(): This teardown method is executed once after all tests of the test class are executed. It is used to destroy the test environment.

The standard special instance methods of the test configuration: setup() and teardown(). setup(): This method is executed before each individual test or before each execution of a test method. It is used, for example, to reset the test doubles before the execution of each test method.

teardown(): This method is executed after each individual test or after each execution of a test method. It will be used to rollback any changes in involved entities Below is a simplified representation of the runtime flow of ABAP Unit test classes.

ABAP Unit test methods: <test method>() represents each unit test method for a given code under test (CUT). Such method is identifiable by the addition FOR TESTING in the method interface.

Travel BO Behavior Pool
Travel BO Behavior Pool

Step 1 Create ABAP unit test class

Create the test class

class icon
class icon
ZRAP100_TC_TRAVEL_EML_### in your previously exercise package.

  1. Right-click on your package

    package icon
    package icon
    ZRAP100_EX### and choose New > ABAP Class from the context menu.

  2. Maintain the required information (### is your group ID) and click Next >.

    • Name: ZRAP100_TC_TRAVEL_EML_###
    • Description: BO Test with EML
  3. Assign a transport request and choose Finish to create the new ABAP class.

    Test Class
    Test Class

  4. Specify the new global ABAP class as an ABAP Unit test class and also specify the test relation to the behavior definition (BDEF) of your Travel BO entity ZRAP100_R_TravelTP_###. For that, insert the addition FOR TESTING RISK LEVEL HARMLESS DURATION SHORT after the addition CREATE PPUBLIC (just before the .) of the class definition to enable the class for ABAP Unit testing.

```ABAP   
FOR TESTING
RISK LEVEL HARMLESS
DURATION SHORT   
```

Then add the ABAP Doc comment provided as code snippet below at the top of class editor to specify the test relation with your behavior definition **`ZRAP100_R_TravelTP_###`** (TADIR entry: `R3TR BDEF`). Replace the placeholder `###` with your group ID.

>**Info**: In ABAP Unit testing, test relations allow the tests of a given object to be executed from the referenced object. In the present example, you will execute the test for your RAP BO in the separate test class. It is also possible to write ABAP Unit tests directly in the behavior implementation classes.

```ABAP
"! @testing BDEF:ZRAP100_R_TravelTP_###
```

Your source code should look like this:

 ![Test Class](https://raw.githubusercontent.com/sap-tutorials/abap-core-development/main/tutorials/abap-environment-rap100-unit-testing/ex8_1.png)
  1. Save
    save icon
    save icon
    the changes.
Step 2 Enhance test class definition
+
Step 3 Implement special methods
+
Step 4 Implement test method
+
Step 5 Run your ABAP unit test
+
Step 6 Test yourself
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 6
1. Create ABAP unit test class 2. Enhance test class definition 3. Implement special methods 4. Implement test method 5. Run your ABAP unit test 6. Test yourself

Learn more →