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 — Disable NPE checks for non-public library fields · Issue #358 · UnitTestBot/UTBotJava

Disable NPE checks for non-public library fields #358

New issue
Copy link
New issue
Copy link
Closed
#353
Closed
Disable NPE checks for non-public library fields#358
#353
Copy link
Assignees
Damtev
Labels
comp-symbolic-engineIssue is related to the symbolic execution engineIssue is related to the symbolic execution engine

Description

@Damtev
Damtev
opened on Jun 30, 2022
Issue body actions

Description

For now, the engine considers method invocations or field access on null values as producing NullPointerException. Previously in #226 such NPE checks were disabled for final fields from library classes because it often leads to the generation of tests with exceptions that are unexpected for a user with high usage of Java reflection. It is suggested to extend such a strategy to non-public fields from library classes too, for the same reasons.

Expected behavior

Consider generating tests for such a method:

public class DateExample {
    boolean foo(Date date) {
        return date.getTime() == 100;
    }
}

Only such 3 tests are expected by user:

@Test
@DisplayName("foo: return date.getTime() == 100 : False -> return date.getTime() == 100")
public void testFoo_DateGetTimeNotEquals100() {
    DateExample dateExample = new DateExample();
    Date date = new Date(-155L);

    boolean actual = dateExample.foo(date);

    assertFalse(actual);
}

@Test
@DisplayName("foo: return date.getTime() == 100 : True -> return date.getTime() == 100")
public void testFoo_DateGetTimeEquals100() {
    DateExample dateExample = new DateExample();
    Date date = new Date(100L);

    boolean actual = dateExample.foo(date);

    assertTrue(actual);
}

@Test
@DisplayName("foo: return date.getTime() == 100 : True -> ThrowNullPointerException")
public void testFoo_DateGetTime() {
    DateExample dateExample = new DateExample();

    assertThrows(NullPointerException.class, () -> dateExample.foo(null));
}

But for now, UtBot also generates at least 2 such tests:

@Test
@DisplayName("foo: return date.getTime() == 100 : True -> ThrowNullPointerException")
public void testFoo_ThrowNullPointerException() throws Exception {
    DateExample dateExample = new DateExample();
    Date date = ((Date) createInstance("java.util.Date"));
    Object immutableGregorianDate = createInstance("sun.util.calendar.ImmutableGregorianDate");
    setField(immutableGregorianDate, "date", null);
    setField(date, "cdate", immutableGregorianDate);

    assertThrows(NullPointerException.class, () -> dateExample.foo(date));
}

@Test
@DisplayName("foo: return date.getTime() == 100 : True -> ThrowNullPointerException")
public void testFoo_ThrowNullPointerException_1() throws ClassNotFoundException, Exception {
    Class dateClazz = Class.forName("java.util.Date");
    BaseCalendar prevGcal = ((BaseCalendar) getStaticFieldValue(dateClazz, "gcal"));
    BaseCalendar prevJcal = ((BaseCalendar) getStaticFieldValue(dateClazz, "jcal"));
    try {
        setStaticField(dateClazz, "gcal", null);
        setStaticField(dateClazz, "jcal", null);
        DateExample dateExample = new DateExample();
        Date date = ((Date) createInstance("java.util.Date"));
        Object immutableGregorianDate = createInstance("sun.util.calendar.ImmutableGregorianDate");
        LocalGregorianCalendar.Date date1 = ((LocalGregorianCalendar.Date) createInstance("sun.util.calendar.LocalGregorianCalendar$Date"));
        setField(date1, "zoneinfo", null);
        setField(date1, "normalized", false);
        setField(date1, "millis", 0);
        setField(date1, "seconds", 0);
        setField(date1, "minutes", 0);
        setField(date1, "hours", 0);
        setField(date1, "dayOfMonth", 0);
        setField(date1, "month", 0);
        setField(date1, "gregorianYear", 11564545);
        setField(immutableGregorianDate, "date", date1);
        setField(date, "cdate", immutableGregorianDate);

        dateExample.foo(date);
    } finally {
        setStaticField(Date.class, "gcal", prevGcal);
        setStaticField(Date.class, "jcal", prevJcal);
    }
}

These tests SHOULD NOT be generated (in the plugin, at least).

Environment

No mocks, Java.

Potential alternatives

Unknown.

Context

We can possibly lose some branches with such disabling.

Activity

  1. Damtev
    added
    comp-symbolic-engineIssue is related to the symbolic execution engineIssue is related to the symbolic execution engine
    on Jun 30, 2022
  2. Damtev
    self-assigned this
    on Jun 30, 2022
  3. Damtev
    added this to UTBot Javaon Jun 30, 2022
  4. Damtev
    moved this to Todo in UTBot Javaon Jun 30, 2022
  5. dtim commented on Jun 30, 2022

    @dtim
    dtim
    on Jun 30, 2022
    Collaborator
    More actions

    Disabling NPE branches for non-public fields (and for final fields as well) affects the fields declared in library classes. While it is OK for JDK classes, it also prevents NPE checks for any third-party libraries, including potentially buggy code. Protected fields may be initialized to null by derived classes as well by any users of these classes using existing constructors or static methods. If the class does not perform necessary checks, NPE may be thrown.

    Attached file: a sample project with a (buggy) library dependency packaged as a jar.

    ProjectWithLib.zip

  6. Damtev
    closed this as completedin #353on Jul 7, 2022
  7. Repository owner moved this from Todo to Done in UTBot Javaon Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @Damtev
    Damtev

Labels

comp-symbolic-engineIssue is related to the symbolic execution engineIssue is related to the symbolic execution engine

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.