Monilnarang opened a new pull request, #7167: URL: https://github.com/apache/incubator-seata/pull/7167
<!-- Please make sure you have read and understood the contributing guidelines --> - [ ] I have registered the PR [changes](../changes). ### Ⅰ. Describe what this PR did **Summary**: - Created 2 separate parameterized tests for `testParse` - Parameterized `testParseThrowException` **Aim**: Improve the test code by avoiding code duplication, improving maintainability, and enhancing readability. By converting the test into a parameterized unit test, we reduce boilerplate code, make it easier to extend by simply adding new input values, and improve debugging by clearly identifying which test case fails instead of searching through individual assertions. **Elaboration**: Like in the below test from DurationUtilTest.java: ``` @Test public void testParse() { Assertions.assertEquals(-1L, DurationUtil.parse("").getSeconds()); Assertions.assertEquals(0L, DurationUtil.parse("8").getSeconds()); Assertions.assertEquals(8L, DurationUtil.parse("8").toMillis()); Assertions.assertEquals(0L, DurationUtil.parse("8ms").getSeconds()); Assertions.assertEquals(8L, DurationUtil.parse("8ms").toMillis()); Assertions.assertEquals(8L, DurationUtil.parse("8s").getSeconds()); Assertions.assertEquals(480L, DurationUtil.parse("8m").getSeconds()); Assertions.assertEquals(28800L, DurationUtil.parse("8h").getSeconds()); Assertions.assertEquals(691200L, DurationUtil.parse("8d").getSeconds()); Assertions.assertEquals(172800L,DurationUtil.parse("P2D").getSeconds()); Assertions.assertEquals(20L,DurationUtil.parse("PT20.345S").getSeconds()); Assertions.assertEquals(20345L,DurationUtil.parse("PT20.345S").toMillis()); Assertions.assertEquals(900L,DurationUtil.parse("PT15M").getSeconds()); Assertions.assertEquals(36000L,DurationUtil.parse("PT10H").getSeconds()); Assertions.assertEquals(8L,DurationUtil.parse("PT8S").getSeconds()); Assertions.assertEquals(86460L,DurationUtil.parse("P1DT1M").getSeconds()); Assertions.assertEquals(183840L,DurationUtil.parse("P2DT3H4M").getSeconds()); Assertions.assertEquals(-21420L,DurationUtil.parse("PT-6H3M").getSeconds()); Assertions.assertEquals(-21780L,DurationUtil.parse("-PT6H3M").getSeconds()); Assertions.assertEquals(21420L,DurationUtil.parse("-PT-6H+3M").getSeconds()); } ``` - The same method calls (`DurationUtil.parse("someString").getSeconds()`, `DurationUtil.parse("someString").toMillis()`) are repeated multiple times with different inputs, making the test harder to maintain and extend. - Both the above method calls are cluttered together in one test, blurring the exact scope of this test. - When a test fails, JUnit only shows which assertion failed, but not which specific input caused the failure. - Adding new test cases requires copying and pasting another Assertions.assertEquals(...) statement instead of simply adding new data. To accomplish this, I have split this test into 2 separate tests (`testParseGetSeconds` and `testParseToMillis`) and retrofitted them into a parameterized unit test. This reduces duplication, allows easy extension by simply adding new value sets, and makes debugging easier as it clearly indicates which test failed instead of requiring a search through individual assertions. ### Ⅱ. Does this pull request fix one issue? <!-- If that, add "fixes #xxx" below in the next line, for example, fixes #97. --> ### Ⅲ. Why don't you add test cases (unit test/integration test)? ### Ⅳ. Describe how to verify it Successful tests run ### Ⅴ. Special notes for reviews -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org