Analyzing and Testing Code

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.

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 Joule→Start 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.

  • 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 Insert to insert the generated code at the current cursor position.

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

Refactoring Generated Test Code

After generating unit tests with Joule, you can refactor the generated code by typing freestyle prompts directly in the Joule Chat.

Refactoring is an important step when using AI to generate unit tests. It ensures that your test code meets your quality standards and fits your preferred coding style. To refactor your generated code, type your instructions as a freestyle prompt in the Joule Chat.

The following examples show what you can ask Joule to do:

  • Avoid hungarian notation (no prefixes)

  • Create CUT in setup method

  • Extract and use parameterized custom assertion

  • Inline and remove custom assertions

  • Program against interfaces instead of classes

  • Use inline declarations

  • Use constructor expressions to fill structures and tables

  • Write assert statements in one line

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.
  • The process involves starting the ABAP Unit Chat, selecting the method(s), choosing a test class, and reviewing/inserting 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.