Generating ABAP Unit Tests with SAP Joule

Objectives

After completing this lesson, you will be able to:
  • Outline the AI-based ABAP Unit features integrated in ADT.
  • Generate ABAP Unit tests using SAP Joule.

AI-Based ABAP Unit Features in ADT

Introduction

As an ABAP developer, you are constantly striving to improve the quality and reliability of your code. Writing thorough unit tests is a cornerstone of this effort, but it can be time-consuming. SAP Joule for Developers, ABAP AI Capabilities can significantly accelerate this process by generating ABAP Unit tests, allowing you to focus on refining and customizing them to ensure comprehensive coverage.

Usage Scenario

SAP Joule for Developers, ABAP AI Capabilities offers powerful assistance in creating ABAP Unit tests, integrated directly into the ABAP Development Tools (ADT) in Eclipse. This AI-driven functionality helps you:

  • Reduce development time: Automatically generate initial test methods, saving you the effort of writing boilerplate code.
  • Improve test coverage: Identify potential test cases and dependencies you might have missed.
  • Enhance code quality: Refactor generated test code to adhere to best practices and coding standards.

Scope of ABAP Unit Test Generation

Joule can generate ABAP Unit tests for:

  • Public, protected, and private methods of global ABAP classes.
  • Public methods of local classes within global ABAP classes.

Features

SAP Joule for Developers, ABAP AI Capabilities provides ABAP developers with AI-based ABAP Unit features integrated into ADT. These features can be accessed through the ABAP Unit Chat and are designed to assist you in creating and improving your tests more effectively. Here is an overview:

  • Test Generation: Generate ABAP Unit tests from various locations within ADT.
  • Dependency Analysis: Identify test-unfriendly dependencies to improve your unit tests.
  • Test Double Support: Isolate test-unfriendly dependencies by replacing them with test doubles.
  • Test Code Explanations: Gain insights with test code explanations to identify test smells and understand test patterns.
  • Generated Test Code Refactoring: Refactor generated test code to meet quality standards and coding style preferences.
  • Splitting Test Classes and Test Methods: Improve code structure by splitting test classes and methods.

Generating ABAP Unit Tests

Generating Tests with the ABAP Unit Chat

Let's walk through an example of generating ABAP Unit tests using Joule and the ABAP Unit Chat.

Note

In addition to what is shown here, you can also use freestyle prompts within the Joule Chat to interact with the AI-powered ABAP Unit features.

Scenario: There is a global ABAP class, ZCL_00_SOLUTION, that contains a local class LCL_CARRIER with a method FIND_PASSENGER_FLIGHT. There are already ABAP Unit tests for this class, which are implemented in test class LTCL_FIND_FLIGHTS.

Starting the ABAP Unit Chat

To generate tests with the ABAP Unit Chat, right-click within the source code editor and select JouleStart Chat for ABAP Unit. The ABAP Unit Chat opens and asks what you want to do with the class.

Generating Tests

If you want to create ABAP Unit tests to validate code, select Generate Tests.

If you want to specifically understand more details about existing ABAP Unit tests, like if there are test smells or which test patterns are used, choose Explain <name of test class>. In our case, the test class has the name LTCL_FIND_FLIGHTS.

If you want to identify test-unfriendly dependencies to improve the test code, choose Find Test-Unfriendly Dependencies.

You can also use simple freestyle prompts by typing in the chat.

As we want to add ABAP Unit tests, we select Generate Tests. In the next step, you have the following options of generating test methods (not shown here):

  • Selected Methods...: Via this option you can select methods of the global class for which you want to generate ABAP Unit tests.

  • Most-Used Methods: This option generates ABAP Unit tests for the most-used methods of the class.

  • Selected Local Class...: This option allows you to select the local class and its corresponding methods for which you want to generate ABAP Unit tests.

Here we choose to generate ABAP Unit tests for method FIND_PASSENGER_FLIGHT of local class LCL_CARRIER.

Adding Tests

In the next step, you can choose between Add Tests to <name of existing test class> (here: LTCL_FIND_FLIGHTS) and Add Tests to New Test Class. We choose the first option.

Reviewing Generated Code

In the next step the generated code is displayed, and we can select one of the buttons above the code:

  • Select Apply to replace the current test code with the generated code. In the dialog that displays, review the differences, then select Finish.

  • Select Insert to insert the generated code at the current cursor position.

  • Select Copy to copy the generated code to the clipboard.

Refactoring Generated Code

Under the area with the generated test code, there is a quick reply named Refactor Generated Code in the Joule Chat. You can use this quick reply to further improve the generated code.

That concludes our example. Joule can generate more complex tests based on the logic of the tested methods. Remember to always review and refine the generated code to ensure it meets your specific testing needs.

Summary

  • SAP Joule for Developers, ABAP AI Capabilities accelerates ABAP Unit test creation, improving code quality and reducing development time.
  • Joule supports ABAP Unit test generation for public, protected, and private methods of global ABAP classes, as well as public methods of local classes within global ABAP classes.
  • The process involves starting the ABAP Unit Chat, selecting the method(s), choosing a test class, and reviewing/applying the generated test code.
  • AI-based ABAP Unit features integrated in ADT include test generation, dependency analysis, test double support, test code explanations, generated test code refactoring, and splitting test classes and test methods.
  • The ABAP Unit Chat allows for freestyle prompts to interact with the AI-powered ABAP Unit features.