Title: Field assertions fixes by Damtev · Pull Request #920 · UnitTestBot/UTBotJava · GitHub
Open Graph Title: Field assertions fixes by Damtev · Pull Request #920 · UnitTestBot/UTBotJava
X Title: Field assertions fixes by Damtev · Pull Request #920 · UnitTestBot/UTBotJava
Description: Description Fixes for several consecutive bugs with field assertions: Unnecessary Object type case for variables created with reflection Missed initial field states for arrays Unnecessary field state assertions for failed tests Also, added line separator after single-line block comment. See corresponding tests behavior in Manual Testing section. Type of Change Bug fix (non-breaking change which fixes an issue) How Has This Been Tested? Automated Testing org.utbot.examples.codegen.modifiers.ClassWithPrivateMutableFieldOfPrivateTypeTest Manual Scenario Consider the following code: public class StringRegex { Pattern pattern = Pattern.compile("\\d{1,10}\\w"); public String stringRegex(String str) { if (pattern.matcher(str).find()) { return str.substring(0, 2); } else { return str; } } } and generate tests for the method stringRegex with the following settings: [Java, JUnit4, No mocks, 60s timeout, useConcreteExecution = true]. One of the generated test methods contains the following code: stringRegex.stringRegex(null); Pattern pattern1 = stringRegex.pattern; Object finalStringRegexPatternCompiled = getFieldValue(pattern1, "java.util.regex.Pattern", "compiled"); Class assertClazz = Class.forName("org.junit.Assert"); Class finalStringRegexPatternCompiledType = boolean.class; Method assertTrueMethod = assertClazz.getDeclaredMethod("assertTrue", finalStringRegexPatternCompiledType); assertTrueMethod.setAccessible(true); Object[] assertTrueMethodArguments = new Object[1]; assertTrueMethodArguments[0] = finalStringRegexPatternCompiled; assertTrueMethod.invoke(null, assertTrueMethodArguments); Here the JUnit4 method assertTrue is invoked using reflection because of variable finalStringRegexPatternCompiled with type Object. But in this example this variable may be safely casted to its type boolean, and reflection usage for the assertTrue usage would be redundant. So, expected code: stringRegex.stringRegex(null); Pattern pattern1 = stringRegex.pattern; boolean finalStringRegexPatternCompiled = ((Boolean) getFieldValue(pattern1, "java.util.regex.Pattern", "compiled")); assertTrue(finalStringRegexPatternCompiled); One of the generated test methods contains the following code: stringRegex.stringRegex(null); Pattern pattern1 = stringRegex.pattern; int[] finalStringRegexPatternBuffer = ((int[]) getFieldValue(pattern1, "java.util.regex.Pattern", "buffer")); Pattern pattern2 = stringRegex.pattern; Map finalStringRegexPatternNamedGroups = ((Map) getFieldValue(pattern2, "java.util.regex.Pattern", "namedGroups")); Pattern pattern3 = stringRegex.pattern; Object finalStringRegexPatternGroupNodes = getFieldValue(pattern3, "java.util.regex.Pattern", "groupNodes"); Pattern pattern4 = stringRegex.pattern; int[] finalStringRegexPatternTemp = ((int[]) getFieldValue(pattern4, "java.util.regex.Pattern", "temp")); Pattern pattern5 = stringRegex.pattern; int finalStringRegexPatternPatternLength = ((Integer) getFieldValue(pattern5, "java.util.regex.Pattern", "patternLength")); assertNull(finalStringRegexPatternNamedGroups); assertEquals(0, finalStringRegexPatternPatternLength); There are no assertions for finalStringRegexPatternNamedGroups and finalStringRegexPatternGroupNodes because they have array types and are filtered out from initial states by mistake. So, expected code is: stringRegex.stringRegex(null); Pattern pattern1 = stringRegex.pattern; boolean finalStringRegexPatternCompiled = ((Boolean) getFieldValue(pattern1, "java.util.regex.Pattern", "compiled")); Pattern pattern2 = stringRegex.pattern; int[] finalStringRegexPatternBuffer = ((int[]) getFieldValue(pattern2, "java.util.regex.Pattern", "buffer")); Pattern pattern3 = stringRegex.pattern; Map finalStringRegexPatternNamedGroups = ((Map) getFieldValue(pattern3, "java.util.regex.Pattern", "namedGroups")); Pattern pattern4 = stringRegex.pattern; List finalStringRegexPatternTopClosureNodes = ((List) getFieldValue(pattern4, "java.util.regex.Pattern", "topClosureNodes")); Pattern pattern5 = stringRegex.pattern; int[] finalStringRegexPatternTemp = ((int[]) getFieldValue(pattern5, "java.util.regex.Pattern", "temp")); Pattern pattern6 = stringRegex.pattern; boolean finalStringRegexPatternHasSupplementary = ((Boolean) getFieldValue(pattern6, "java.util.regex.Pattern", "hasSupplementary")); assertTrue(finalStringRegexPatternCompiled); assertNull(finalStringRegexPatternBuffer); assertNull(finalStringRegexPatternNamedGroups); assertNull(finalStringRegexPatternTopClosureNodes); assertNull(finalStringRegexPatternTemp); assertTrue(finalStringRegexPatternHasSupplementary); that contains assertions for these fields. Actually, the execution above is exception-throwing and breaks after invocation of method under test stringRegex.stringRegex(string);. So, all these field assertions are redundant because they will never be invoked, and the last statement of this test method has to be MUT invocation. Checklist (remove irrelevant options): The change followed the style guidelines of the UTBot project Self-review of the code is passed The change contains enough commentaries, particularly in hard-to-understand areas New documentation is provided or existed one is altered No new warnings New tests have been added All tests pass locally with my changes
Open Graph Description: Description Fixes for several consecutive bugs with field assertions: Unnecessary Object type case for variables created with reflection Missed initial field states for arrays Unnecessary field st...
X Description: Description Fixes for several consecutive bugs with field assertions: Unnecessary Object type case for variables created with reflection Missed initial field states for arrays Unnecessary field st...
Opengraph URL: https://github.com/UnitTestBot/UTBotJava/pull/920
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:f62de403-6474-9437-1b0b-1f9a080bcd7a |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | E9C0:38DCC4:305F92B:4198CB5:698DC663 |
| html-safe-nonce | 44e7cb9559f7cf573f73cecd80062a29787c024dc828afe236d1ec45eb8ab301 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOUMwOjM4RENDNDozMDVGOTJCOjQxOThDQjU6Njk4REM2NjMiLCJ2aXNpdG9yX2lkIjoiNTQ0Mjk5MjkzNDc5NzI5NzI1MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 067129d9184f5d699fd6920a3718b8b98c4904b45ecb873034d566915ca0b9e4 |
| hovercard-subject-tag | pull_request:1054925878 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/UnitTestBot/UTBotJava/pull/920/files |
| twitter:image | https://avatars.githubusercontent.com/u/39625503?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/39625503?s=400&v=4 |
| og:image:alt | Description Fixes for several consecutive bugs with field assertions: Unnecessary Object type case for variables created with reflection Missed initial field states for arrays Unnecessary field st... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2457fd9a1b17ca7f38c3c2653895ccb2e0dd04f18f77446a21354e9bc4b7902f |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 878672b82363fd98fbdc5bd0edb68e572c8a8d9b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width