Title: Disable NPE checks for non-public library fields · Issue #358 · UnitTestBot/UTBotJava · GitHub
Open Graph Title: Disable NPE checks for non-public library fields · Issue #358 · UnitTestBot/UTBotJava
X Title: Disable NPE checks for non-public library fields · Issue #358 · UnitTestBot/UTBotJava
Description: 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 ...
Open Graph Description: 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...
X Description: 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...
Opengraph URL: https://github.com/UnitTestBot/UTBotJava/issues/358
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Disable NPE checks for non-public library fields","articleBody":"**Description**\r\n\r\nFor 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.\r\n\r\n**Expected behavior**\r\n\r\nConsider generating tests for such a method:\r\n```java\r\npublic class DateExample {\r\n boolean foo(Date date) {\r\n return date.getTime() == 100;\r\n }\r\n}\r\n```\r\n\r\nOnly such 3 tests are expected by user:\r\n```java\r\n@Test\r\n@DisplayName(\"foo: return date.getTime() == 100 : False -\u003e return date.getTime() == 100\")\r\npublic void testFoo_DateGetTimeNotEquals100() {\r\n DateExample dateExample = new DateExample();\r\n Date date = new Date(-155L);\r\n\r\n boolean actual = dateExample.foo(date);\r\n\r\n assertFalse(actual);\r\n}\r\n\r\n@Test\r\n@DisplayName(\"foo: return date.getTime() == 100 : True -\u003e return date.getTime() == 100\")\r\npublic void testFoo_DateGetTimeEquals100() {\r\n DateExample dateExample = new DateExample();\r\n Date date = new Date(100L);\r\n\r\n boolean actual = dateExample.foo(date);\r\n\r\n assertTrue(actual);\r\n}\r\n\r\n@Test\r\n@DisplayName(\"foo: return date.getTime() == 100 : True -\u003e ThrowNullPointerException\")\r\npublic void testFoo_DateGetTime() {\r\n DateExample dateExample = new DateExample();\r\n\r\n assertThrows(NullPointerException.class, () -\u003e dateExample.foo(null));\r\n}\r\n```\r\n\r\nBut for now, UtBot also generates at least 2 such tests:\r\n```java\r\n@Test\r\n@DisplayName(\"foo: return date.getTime() == 100 : True -\u003e ThrowNullPointerException\")\r\npublic void testFoo_ThrowNullPointerException() throws Exception {\r\n DateExample dateExample = new DateExample();\r\n Date date = ((Date) createInstance(\"java.util.Date\"));\r\n Object immutableGregorianDate = createInstance(\"sun.util.calendar.ImmutableGregorianDate\");\r\n setField(immutableGregorianDate, \"date\", null);\r\n setField(date, \"cdate\", immutableGregorianDate);\r\n\r\n assertThrows(NullPointerException.class, () -\u003e dateExample.foo(date));\r\n}\r\n\r\n@Test\r\n@DisplayName(\"foo: return date.getTime() == 100 : True -\u003e ThrowNullPointerException\")\r\npublic void testFoo_ThrowNullPointerException_1() throws ClassNotFoundException, Exception {\r\n Class dateClazz = Class.forName(\"java.util.Date\");\r\n BaseCalendar prevGcal = ((BaseCalendar) getStaticFieldValue(dateClazz, \"gcal\"));\r\n BaseCalendar prevJcal = ((BaseCalendar) getStaticFieldValue(dateClazz, \"jcal\"));\r\n try {\r\n setStaticField(dateClazz, \"gcal\", null);\r\n setStaticField(dateClazz, \"jcal\", null);\r\n DateExample dateExample = new DateExample();\r\n Date date = ((Date) createInstance(\"java.util.Date\"));\r\n Object immutableGregorianDate = createInstance(\"sun.util.calendar.ImmutableGregorianDate\");\r\n LocalGregorianCalendar.Date date1 = ((LocalGregorianCalendar.Date) createInstance(\"sun.util.calendar.LocalGregorianCalendar$Date\"));\r\n setField(date1, \"zoneinfo\", null);\r\n setField(date1, \"normalized\", false);\r\n setField(date1, \"millis\", 0);\r\n setField(date1, \"seconds\", 0);\r\n setField(date1, \"minutes\", 0);\r\n setField(date1, \"hours\", 0);\r\n setField(date1, \"dayOfMonth\", 0);\r\n setField(date1, \"month\", 0);\r\n setField(date1, \"gregorianYear\", 11564545);\r\n setField(immutableGregorianDate, \"date\", date1);\r\n setField(date, \"cdate\", immutableGregorianDate);\r\n\r\n dateExample.foo(date);\r\n } finally {\r\n setStaticField(Date.class, \"gcal\", prevGcal);\r\n setStaticField(Date.class, \"jcal\", prevJcal);\r\n }\r\n}\r\n```\r\n\r\nThese tests SHOULD NOT be generated (in the plugin, at least).\r\n\r\n**Environment**\r\n\r\nNo mocks, Java.\r\n\r\n**Potential alternatives**\r\n\r\nUnknown.\r\n\r\n**Context**\r\n\r\nWe can possibly lose some branches with such disabling.\r\n","author":{"url":"https://github.com/Damtev","@type":"Person","name":"Damtev"},"datePublished":"2022-06-30T10:05:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/358/UTBotJava/issues/358"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:11eeda8b-74bb-fe13-ce27-5f582664a04b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A86C:146116:9A8FC:D66B6:698E0619 |
| html-safe-nonce | ba90979f455ba733b5ee003e7142e4f1b7d56d893ceb5412197ae7234ea70e69 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODZDOjE0NjExNjo5QThGQzpENjZCNjo2OThFMDYxOSIsInZpc2l0b3JfaWQiOiIyODAyNjA5MTQ2MTY3MTY2NDg5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 44d9235a17d37713da5839e7cf4b132bc64713815df09d5ce4aa12f0773d176d |
| hovercard-subject-tag | issue:1289901101 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/UnitTestBot/UTBotJava/358/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1d12a496109a614bf593054841d951cbe7bad83eeed8523cfafd8754abc2541f/UnitTestBot/UTBotJava/issues/358 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1d12a496109a614bf593054841d951cbe7bad83eeed8523cfafd8754abc2541f/UnitTestBot/UTBotJava/issues/358 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Damtev |
| hostname | github.com |
| expected-hostname | github.com |
| None | 70a88bfe6d9639434ae7f5a46d15c336b8884978a51526bc55fc57d848f1e3c1 |
| turbo-cache-control | no-preview |
| go-import | github.com/UnitTestBot/UTBotJava git https://github.com/UnitTestBot/UTBotJava.git |
| octolytics-dimension-user_id | 87413538 |
| octolytics-dimension-user_login | UnitTestBot |
| octolytics-dimension-repository_id | 480810501 |
| octolytics-dimension-repository_nwo | UnitTestBot/UTBotJava |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 480810501 |
| octolytics-dimension-repository_network_root_nwo | UnitTestBot/UTBotJava |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2519b16db5550494a653f9a8837c14ef7df80804 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width