Title: Useless assignment of enum values to corresponding static fields in generated tests · Issue #618 · UnitTestBot/UTBotJava · GitHub
Open Graph Title: Useless assignment of enum values to corresponding static fields in generated tests · Issue #618 · UnitTestBot/UTBotJava
X Title: Useless assignment of enum values to corresponding static fields in generated tests · Issue #618 · UnitTestBot/UTBotJava
Description: Description Generated tests explicitly assign enum values to corresponding static fields, which is useless as the set of enum values is fixed, and no new instances can be created. To Reproduce Generate a test suite for the Coin.reverse()...
Open Graph Description: Description Generated tests explicitly assign enum values to corresponding static fields, which is useless as the set of enum values is fixed, and no new instances can be created. To Reproduce Gene...
X Description: Description Generated tests explicitly assign enum values to corresponding static fields, which is useless as the set of enum values is fixed, and no new instances can be created. To Reproduce Gene...
Opengraph URL: https://github.com/UnitTestBot/UTBotJava/issues/618
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Useless assignment of enum values to corresponding static fields in generated tests","articleBody":"**Description**\r\n\r\nGenerated tests explicitly assign enum values to corresponding static fields, which is useless as the set of enum values is fixed, and no new instances can be created.\r\n\r\n**To Reproduce**\r\n\r\nGenerate a test suite for the `Coin.reverse()` method in the following code.\r\n```java\r\npublic enum Coin {\r\n HEADS,\r\n TAILS;\r\n\r\n public Coin reverse() {\r\n return this == HEADS ? TAILS : HEADS;\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior**\r\n\r\nGenerated tests should not contain assignments of enum values to corresponding static fields.\r\n\r\n**Actual behavior**\r\n\r\nGenerated tests explicitly assign enum values to corresponding static fields using reflection.\r\n\r\nNote: this behavior depends on the enum support that is not in `main` yet (PR #611). When checking on `main`, the behavior may differ.\r\n\r\n**Visual proofs (screenshots, logs, images)**\r\n\r\nGenerated tests:\r\n```java\r\n ///region SUCCESSFUL EXECUTIONS for method reverse()\r\n\r\n /**\r\n * \u003cpre\u003e\r\n * Test executes conditions:\r\n * {@code (this == HEADS): False }\r\n * returns from: {@code return this == HEADS ? TAILS : HEADS; }\r\n * \u003c/pre\u003e\r\n */\r\n @Test\r\n //@org.junit.jupiter.api.DisplayName(\"reverse: this == HEADS : False -\u003e return this == HEADS ? TAILS : HEADS\")\r\n public void testReverse_NotEqualsHEADS() throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException {\r\n Coin prevHEADS = Coin.HEADS;\r\n try {\r\n Coin heads = Coin.HEADS;\r\n Class coinClazz = Class.forName(\"enums.Coin\");\r\n setStaticField(coinClazz, \"HEADS\", heads);\r\n Coin coin = Coin.TAILS;\r\n\r\n Coin actual = coin.reverse();\r\n\r\n\r\n assertEquals(heads, actual);\r\n } finally {\r\n setStaticField(Coin.class, \"HEADS\", prevHEADS);\r\n }\r\n }\r\n\r\n /**\r\n * \u003cpre\u003e\r\n * Test executes conditions:\r\n * {@code (this == HEADS): True }\r\n * returns from: {@code return this == HEADS ? TAILS : HEADS; }\r\n * \u003c/pre\u003e\r\n */\r\n @Test\r\n //@org.junit.jupiter.api.DisplayName(\"reverse: this == HEADS : True -\u003e return this == HEADS ? TAILS : HEADS\")\r\n public void testReverse_EqualsHEADS() throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException {\r\n Coin prevHEADS = Coin.HEADS;\r\n Coin prevTAILS = Coin.TAILS;\r\n try {\r\n Coin heads = Coin.HEADS;\r\n Class coinClazz = Class.forName(\"enums.Coin\");\r\n setStaticField(coinClazz, \"HEADS\", heads);\r\n Coin tails = Coin.TAILS;\r\n setStaticField(coinClazz, \"TAILS\", tails);\r\n\r\n Coin actual = heads.reverse();\r\n\r\n\r\n assertEquals(tails, actual);\r\n } finally {\r\n setStaticField(Coin.class, \"HEADS\", prevHEADS);\r\n setStaticField(Coin.class, \"TAILS\", prevTAILS);\r\n }\r\n }\r\n ///endregion\r\n```\r\n\r\n**Environment**\r\n\r\nThis behavior does not depend on any specific test environment.\r\n\r\n**Additional context**\r\n\r\nAssignment of specific values to static fields (usually using reflection) and restoring old values after the test is a common trait of UTBot-generated tests. Generally this behavior is necessary as the codegen should guarantee that the initial state of each object matches the expected initial state induced by the symbolic engine. On the other hand, often these assignment are useless. For example, final static fields of JDK classes are correctly initialized by the JDK code, and there is no sensible way to assign something else to these fields without risking to break things. Unfortunately, it seems that the correct and non-redundant object initialization is hard.\r\n\r\nMaybe it might be done for some special cases like enum value initialization, as we can be sure that each corresponding static field already has the correct value at the start of the test.","author":{"url":"https://github.com/dtim","@type":"Person","name":"dtim"},"datePublished":"2022-07-30T20:23:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/618/UTBotJava/issues/618"}
| 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:a51dc5ed-44d7-a4aa-d125-f407a306ffab |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A83C:1F69FA:2182AA2:2AFFA3C:69901344 |
| html-safe-nonce | 670cb7bc96d7db9ae6646a6727d2d6de0268f5dd60cf0daa3eb8792fe5e884fd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODNDOjFGNjlGQToyMTgyQUEyOjJBRkZBM0M6Njk5MDEzNDQiLCJ2aXNpdG9yX2lkIjoiNTgzNDcxNTA3NDg0MTgwOTczMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f3627532dc54c2d7312d463a0a66645097181b13623303595771b31ad378db9f |
| hovercard-subject-tag | issue:1323276175 |
| 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/618/issue_layout |
| twitter:image | https://opengraph.githubassets.com/df6b7de447aa91fe4110c93452680921ef1b313328b22ea3ad19aa586466b31e/UnitTestBot/UTBotJava/issues/618 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/df6b7de447aa91fe4110c93452680921ef1b313328b22ea3ad19aa586466b31e/UnitTestBot/UTBotJava/issues/618 |
| og:image:alt | Description Generated tests explicitly assign enum values to corresponding static fields, which is useless as the set of enum values is fixed, and no new instances can be created. To Reproduce Gene... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dtim |
| hostname | github.com |
| expected-hostname | github.com |
| None | 42c603b9d642c4a9065a51770f75e5e27132fef0e858607f5c9cb7e422831a7b |
| 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 | 3b33c5aedc9808f45bc5fcf0b1e4404cf749dac7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width