Article

Quality assurance with IBM watsonx Code Assistant

How watsonx Code Assistant revolutionizes unit testing

By

Shwetha BR

Software testing is often considered to constitute up to 50% of product development costs, as it encompasses various critical testing activities. Testing ensures that modifications or updates to a codebase do not adversely impact its functionality. However, understanding effective unit tests, writing test suites, and maintaining those test suites becomes increasingly complex and expensive, especially for large projects.

Unit testing provides a way to identify the largest proportion of software errors at the point where it is easiest, fastest, and cheapest to address them.

However, unit testing can also be:

  • Difficult. Writing precise test cases for intricate code can be challenging.
  • Time-Consuming. Manually creating tests takes a significant amount of development time.
  • Expensive. Without automated unit testing tools, costs add up quickly.

IBM watsonx Code Assistant revolutionizes the testing process by leveraging the power of AI to automate and optimize this difficult, time consuming, and expensive process.

Here's how watsonx Code Assistant ensures faster, more reliable testing while reducing costs:

  • Automated test generation
  • Real-time code validation
  • Semantic equivalence validation

Automated test generation

The capability analyzes code and automatically generates unit test cases, helping you:

  • Validate Logic: Ensures the correctness of functions and classes.
  • Cover Edge Cases: Includes scenarios that might not be immediately obvious, such as boundary values or rare input combinations.
  • Maximize Code Coverage: Ensures all critical paths in the code are thoroughly tested.

For example, the generated tests for format(123, 5) ensure the result is "123", validating typical cases. It also handles edge cases like format(12345, 5) to return "12345", covering boundary conditions. This ensures robust, efficient, and comprehensive testing.

Test data analysis

Real-time code validation

Real-time code validation includes instant feedback (code explanation) and error detection.

Instant Feedback

As you type in your favorite IDE, WCA offers in-editor code completion capabilities, including: Single-line completion, Multi-line completion and Comment-to-code transformation.

Code explanation

As shown in the previous figure:

User user = new User(registerParam.getEmail(),
                    registerParam.getUsername(),
                    passwordEncoder.encode(registerParam.getPassword()),
                    "",
                         defaultAvatar);

For example, inline suggestions provide method autocompletions for registerParam.getEmail() or passwordEncoder.encode(). These reduce errors like mistyping method names and help ensure the parameters align with the constructor.

Error Detection

Errors post-generation are identified and flagged, enabling quick resolution before moving to production.

By using an intuitive natural language interface, developers can quickly resolve issues. In the following figure, the red underline appears under the registerParam or User instantiation, indicating a syntax error.

Code fixes

WCA can facilitate a conversation-like experience where the model helps developers resolve the issue.

Semantic equivalence validation

This feature is particularly beneficial for mainframe applications using the watsonx Code Assistant for Z.

  • WCA semantic equivalence testing: Provides an automated and comprehensive way to ensure that the COBOL and Java versions of a program work the same for all possible inputs.
  • Enhance eompatibility: Optimize COBOL components to align with current performance standards and ensure seamless integration with modernized Java systems.

Here's how the equivalence test is performed with the output.

equivalence.png

For example, semantic equivalence ensured the following COBOL example program, BONUS-CALCULATOR, which calculates a 10% bonus, worked identically when migrated to Java. The test validated both outputs matched (5000) for the same input (50000). This ensured the original logic was preserved, maintaining accuracy and reliability during modernization, with minimal errors in the transformation process.

COBOL program

IDENTIFICATION DIVISION.
PROGRAM-ID. BONUS-CALCULATOR.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMPLOYEE-SALARY       PIC 9(5) VALUE 50000.
01 BONUS                PIC 9(4).

PROCEDURE DIVISION.
    COMPUTE BONUS = EMPLOYEE-SALARY * 0.10.
    DISPLAY 'BONUS: ' BONUS.
    STOP RUN.

Java program

@Test
public void testCalculateBonus() {
    // Input from COBOL test case
    int salary = 50000;

    // Expected output from COBOL program
    int expectedBonus = 5000;

    // Call the translated Java method
    int actualBonus = BonusCalculator.calculateBonus(salary);

    // Assert the result matches the COBOL output
    assertEquals(expectedBonus, actualBonus, "The calculated bonus should match the COBOL output.");
}

Managing dependencies

Handling dependencies in code can be a significant challenge. IBM watsonx Code Assistant simplifies dependency management with these capabilities:

  • Mocking and Stubbing. Automatically generating mock objects or stubs for external dependencies, such as database calls or APIs.

  • Isolation Testing. Enables developers to test their code in isolation without requiring access to live systems or services.

By using mocking, external dependencies like database systems or APIs do not need to be connected. This ensures that the tests focus entirely on the logic within the code being tested.

Mocking

For example, watsonx Code Assistant automates mocking for Connection, PreparedStatement, and ResultSet objects, ensuring unit tests run without a live database. It simulates database outputs for ResultSet, and ensures modernized Java code behaves like the legacy COBOL equivalent.

Summary

Unit testing is a critical aspect of modern software development, but its challenges (complexity, time consumption, and cost) can impede progress, especially for large-scale or legacy systems. IBM watsonx Code Assistant addresses these hurdles with AI-driven capabilities.

By automating test generation, enabling real-time validation, ensuring semantic equivalence, and simplifying dependency handling, IBM watsonx Code Assistant empowers developers to focus on innovation while maintaining robust, error-free codebases.

Whether modernizing legacy systems or developing cutting-edge applications, watsonx Code Assistant ensures reliability, efficiency, and scalability in software testing lifecycle.