GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Test generation fails for Optional<T> class in the plugin · Issue #226 · UnitTestBot/UTBotJava

Test generation fails for Optional<T> class in the plugin #226

New issue
Copy link
New issue
Copy link
Closed
#305
Closed
Test generation fails for Optional<T> class in the plugin#226
#305
Copy link
Assignees
dtim
Labels
comp-symbolic-engineIssue is related to the symbolic execution engineIssue is related to the symbolic execution enginectg-bugIssue is a bugIssue is a bugpriority-top-focusTop priority chosen by dev teamTop priority chosen by dev team

Description

@dtim
dtim
opened on Jun 16, 2022
Issue body actions

Description

No test cases are generated for methods involving Java Optional<T> class. UtErrors are generated by the engine during traverse due to ClassNotFound exception: UtOptional class can't be loaded. This behavior may be reproduced using the Idea plugin, but models are successfully generated for the same code in the engine unit tests.

To Reproduce

Generate unit tests for the sample method:

import java.util.Optional;

public class OptionalExamples {
    public Optional<Integer> nonEmptyIfPositive(int n) {
        if (n > 0) {
            return Optional.of(n);
        } else {
            return Optional.empty();
        }
    }
}

Expected behavior

Two tests are generated, one for each branch in the code (see Additional context for more details).

Actual behavior

Errors are reported:

public class OptionalExamplesTest {
    ///region Test suites for executable collections.OptionalExamples.nonEmptyIfPositive


    ///region Errors report for nonEmptyIfPositive

    public void testNonEmptyIfPositive_errors() {
        // Couldn't generate some tests. List of errors:
        // 
        // 2 occurrences of:
        // org.utbot.engine.overrides.collections.UtOptional

    }
    ///endregion

    ///endregion
}

Environment

Mockito: any configuration. Test framework: JUnit5.

Additional context

The primary error reason: UtOptional wrapper is used instead of Optional, but the class loader used by the test generator in the plugin can't find it. It's OK (engine-specific wrappers should never leak to the user), but shouldMock function checks the class and fails with ClassNotFound exception.

Adding a check to shouldMock fixes the exception, but the execution path corresponding to Optional.empty() return value is lost unless checkNpeForFinalFields setting is set to true (it is false by default). See SootField.shouldBeNotNull() declared in Extensions.kt.

A similar code sample that uses OptionalInt instead of Optional<Integer> is processed without any problems, because primitive field types are never mocked so no class check is performed.

Code sample:

public OptionalInt nonEmptyOptionalIntIfPositive(int n) {
    if (n > 0) {
        return OptionalInt.of(n);
    } else {
        return OptionalInt.empty();
    }
}

Generated tests:

///region SUCCESSFUL EXECUTIONS for method nonEmptyOptionalIntIfPositive(int)

/**
 * <pre>
 * Test executes conditions:
 *     {@code (n > 0): True }
 * invokes:
 *     OptionalInt::of once
 * returns from: {@code return OptionalInt.of(n); }
 * </pre>
 */
@Test
@DisplayName("nonEmptyOptionalIntIfPositive: n > 0 : True -> return OptionalInt.of(n)")
public void testNonEmptyOptionalIntIfPositive_NGreaterThanZero() {
    OptionalExamples optionalExamples = new OptionalExamples();

    OptionalInt actual = optionalExamples.nonEmptyOptionalIntIfPositive(1);

    OptionalInt expected = of(1);

    assertTrue(deepEquals(expected, actual));
}

/**
 * <pre>
 * Test executes conditions:
 *     {@code (n > 0): False }
 * </pre>
 */
@Test
@DisplayName("nonEmptyOptionalIntIfPositive: -> n > 0 : False")
public void testNonEmptyOptionalIntIfPositive_NLessOrEqualZero() throws ClassNotFoundException, Exception {
    Class optionalIntClazz = Class.forName("java.util.OptionalInt");
    OptionalInt prevEMPTY = ((OptionalInt) getStaticFieldValue(optionalIntClazz, "EMPTY"));
    try {
        OptionalInt empty = empty();
        setStaticField(optionalIntClazz, "EMPTY", empty);
        OptionalExamples optionalExamples = new OptionalExamples();

        OptionalInt actual = optionalExamples.nonEmptyOptionalIntIfPositive(0);


        assertTrue(deepEquals(empty, actual));
    } finally {
        setStaticField(OptionalInt.class, "EMPTY", prevEMPTY);
    }
}
///endregion

Activity

  1. dtim
    added
    ctg-bugIssue is a bugIssue is a bug
    comp-symbolic-engineIssue is related to the symbolic execution engineIssue is related to the symbolic execution engine
    on Jun 16, 2022
  2. dtim
    self-assigned this
    on Jun 16, 2022
  3. korifey
    added this to UTBot Javaon Jun 16, 2022
  4. korifey
    moved this to Todo in UTBot Javaon Jun 16, 2022
  5. dtim
    moved this from Todo to In Progress in UTBot Javaon Jun 21, 2022
  6. denis-fokin
    added
    priority-top-focusTop priority chosen by dev teamTop priority chosen by dev team
    on Jun 22, 2022
  7. dtim
    added 7 commits that reference this issue on Jun 24, 2022

    Test generation for Optional<T> in the plugin (#226)

    333d563

    Test generation for Optional<T> in the plugin (#226)

    2840303

    Test generation for Optional<T> in the plugin (#226)

    d5b45d8

    Test generation for Optional<T> in the plugin (#226)

    3da10eb

    Test generation for Optional<T> in the plugin (#226)

    273776c

    Test generation for Optional<T> in the plugin (#226)

    2e43190

    Test generation for Optional<T> in the plugin (#226)

    b97803d
  8. dtim
    closed this as completedin #305on Jun 28, 2022
  9. dtim
    added a commit that references this issue on Jun 28, 2022

    Test generation for Optional<T> in the plugin (#226)

    7242bb5
  10. Repository owner moved this from In Progress to Done in UTBot Javaon Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @dtim
    dtim

Labels

comp-symbolic-engineIssue is related to the symbolic execution engineIssue is related to the symbolic execution enginectg-bugIssue is a bugIssue is a bugpriority-top-focusTop priority chosen by dev teamTop priority chosen by dev team

Type

No type

Projects

  • UTBot Java
    Status
    Done
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.