Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to JUnit 5 #135

Merged
merged 1 commit into from
Aug 17, 2024
Merged

Migrate to JUnit 5 #135

merged 1 commit into from
Aug 17, 2024

Conversation

jodastephen
Copy link
Member

@jodastephen jodastephen commented Aug 17, 2024

Summary by CodeRabbit

  • New Features

    • Upgraded to JUnit 5 for improved testing capabilities across all test classes.
    • Introduced modern assertions and annotations enhancing readability and maintainability.
  • Bug Fixes

    • Updated test methods to use assertThrows and assertDoesNotThrow for clearer exception handling.
  • Refactor

    • Removed the public access modifier from test methods to follow best practices in JUnit 5.
  • Documentation

    • Enhanced comments and method names for better clarity and understanding of test intentions.

These changes collectively modernise the testing framework and improve the robustness of our test suite.

Copy link

coderabbitai bot commented Aug 17, 2024

Walkthrough

Walkthrough

The recent updates involve a significant migration from JUnit 4 to JUnit 5 across multiple test classes in the org.joda.money package. This transition enhances the clarity and functionality of the test suite by adopting modern testing practices, improving assertion methods, and refining method visibility. The changes reflect a comprehensive effort to modernise the test framework, ensuring consistency and maintainability throughout the codebase.

Changes

Files Change Summary
pom.xml Upgraded JUnit dependency from 4.13.2 to 5.11.0, changed group and artifact identifiers, and removed junit-dataprovider.
src/test/java/org/joda/money/TestCurrencyMismatchException.java, src/test/java/org/joda/money/TestCurrencyUnit.java, src/test/java/org/joda/money/TestCurrencyUnitExtension.java, src/test/java/org/joda/money/TestIllegalCurrencyException.java, src/test/java/org/joda/money/TestMoneyUtils_BigMoney.java, src/test/java/org/joda/money/TestMoneyUtils_Money.java, src/test/java/org/joda/money/TestStringConvert.java, src/test/java/org/joda/money/format/TestMoneyAmountStyle.java, src/test/java/org/joda/money/format/TestMoneyFormatter.java, src/test/java/org/joda/money/format/TestMoneyFormatterBuilder.java, src/test/java/org/joda/money/format/TestMoneyFormatterException.java, src/test/java/org/joda/money/format/TestMoneyParseContext.java Transitioned to JUnit 5: updated imports, changed public methods to package-private, improved assertions (e.g., replaced assertEquals with assertTrue, assertNull), and used assertThrows for exception handling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant TestSuite
    participant JUnit5

    User->>TestSuite: Execute Tests
    TestSuite->>JUnit5: Run Test Cases
    JUnit5-->>TestSuite: Return Results
    TestSuite-->>User: Display Results
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
src/test/java/org/joda/money/TestCurrencyUnit.java (1)

Line range hint 523-552:
Security warning: Avoid using ObjectInputStream.

The use of ObjectInputStream can lead to security vulnerabilities such as remote code execution. Consider safer alternatives or ensure the data being deserialized is trusted.

- ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
+ // Consider using a safer deserialization mechanism or validating the input data.

Also applies to: 556-573

Tools
ast-grep

[warning] 541-541: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8


[warning] 562-562: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3e18222 and 0d8ce2c.

Files selected for processing (13)
  • pom.xml (2 hunks)
  • src/test/java/org/joda/money/TestCurrencyMismatchException.java (2 hunks)
  • src/test/java/org/joda/money/TestCurrencyUnit.java (27 hunks)
  • src/test/java/org/joda/money/TestCurrencyUnitExtension.java (6 hunks)
  • src/test/java/org/joda/money/TestIllegalCurrencyException.java (1 hunks)
  • src/test/java/org/joda/money/TestMoneyUtils_BigMoney.java (8 hunks)
  • src/test/java/org/joda/money/TestMoneyUtils_Money.java (7 hunks)
  • src/test/java/org/joda/money/TestStringConvert.java (1 hunks)
  • src/test/java/org/joda/money/format/TestMoneyAmountStyle.java (23 hunks)
  • src/test/java/org/joda/money/format/TestMoneyFormatter.java (11 hunks)
  • src/test/java/org/joda/money/format/TestMoneyFormatterBuilder.java (18 hunks)
  • src/test/java/org/joda/money/format/TestMoneyFormatterException.java (1 hunks)
  • src/test/java/org/joda/money/format/TestMoneyParseContext.java (3 hunks)
Additional context used
ast-grep
src/test/java/org/joda/money/TestCurrencyUnit.java

[warning] 541-541: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8


[warning] 562-562: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8

Additional comments not posted (53)
src/test/java/org/joda/money/format/TestMoneyFormatterException.java (5)

18-19: Good use of JUnit 5 assertions.

The transition to assertThrows and assertDoesNotThrow enhances clarity and aligns with modern testing practices.


23-23: JUnit 5 annotation update is appropriate.

Updating the @Test annotation to JUnit 5 is a necessary step in the migration process.


28-28: Visibility reduction is appropriate for test classes.

Changing the class to package-private is a good practice as test classes do not need to be public.


30-33: Correct use of assertThrows.

Replacing the expected parameter with assertThrows improves the readability and clarity of the test.


37-39: Correct use of assertDoesNotThrow.

Using assertDoesNotThrow appropriately checks that no exception is thrown, improving test clarity.

src/test/java/org/joda/money/TestIllegalCurrencyException.java (5)

18-19: Good transition to JUnit 5 assertions.

Switching to assertEquals and assertNull from JUnit 5 improves the clarity and functionality of the tests.


21-21: JUnit 5 annotation update is appropriate.

Updating the @Test annotation to JUnit 5 is a necessary step in the migration process.


26-26: Visibility reduction is appropriate for test classes.

Changing the class to package-private is a good practice as test classes do not need to be public.


32-35: Use of assertNull is semantically appropriate.

Replacing assertEquals with assertNull for null checks is a more semantically correct approach.


39-42: Appropriate use of assertNull for null checks.

Using assertNull for both message and cause checks improves test clarity and correctness.

src/test/java/org/joda/money/TestStringConvert.java (6)

18-18: Good transition to JUnit 5 assertions.

Switching to assertEquals from JUnit 5 improves the clarity and functionality of the tests.


22-22: JUnit 5 annotation update is appropriate.

Updating the @Test annotation to JUnit 5 is a necessary step in the migration process.


27-27: Visibility reduction is appropriate for test classes.

Changing the class to package-private is a good practice as test classes do not need to be public.


30-33: Correct parameter order in assertEquals.

Ensuring the expected value is first in assertEquals calls aligns with best practices.


38-41: Correct parameter order in assertEquals.

Ensuring the expected value is first in assertEquals calls aligns with best practices.


46-49: Correct parameter order in assertEquals.

Ensuring the expected value is first in assertEquals calls aligns with best practices.

src/test/java/org/joda/money/TestCurrencyMismatchException.java (4)

18-19: Update imports to JUnit 5.

The import statements have been correctly updated to use JUnit 5's Assertions class. This change aligns with modern testing practices.


21-21: Use of JUnit 5 @test annotation.

The @Test annotation is now sourced from org.junit.jupiter.api.Test, which is appropriate for JUnit 5.


26-26: Visibility change for the test class.

The class TestCurrencyMismatchException has been changed from public to package-private. This is generally acceptable for test classes unless they need to be accessed from other packages.

Ensure that this change does not affect any external test runners or frameworks that might require public visibility.


35-67: Improved assertion methods.

The assertions have been updated to use assertNull, which improves readability and aligns with JUnit 5 best practices. The method visibility has been changed to package-private, which is consistent with JUnit 5 conventions.

src/test/java/org/joda/money/TestCurrencyUnitExtension.java (4)

18-20: Update imports to JUnit 5.

The import statements have been correctly updated to use JUnit 5's Assertions class. This change aligns with modern testing practices.


24-24: Use of JUnit 5 @test annotation.

The @Test annotation is now sourced from org.junit.jupiter.api.Test, which is appropriate for JUnit 5.


29-29: Visibility change for the test class.

The class TestCurrencyUnitExtension has been changed from public to package-private. This is generally acceptable for test classes unless they need to be accessed from other packages.

Ensure that this change does not affect any external test runners or frameworks that might require public visibility.


Line range hint 32-94:
Improved assertion methods.

The assertions have been updated to use assertTrue, which improves readability and aligns with JUnit 5 best practices. The method visibility has been changed to package-private, which is consistent with JUnit 5 conventions.

src/test/java/org/joda/money/format/TestMoneyParseContext.java (4)

18-21: Update imports to JUnit 5.

The import statements have been correctly updated to use JUnit 5's Assertions class. This change aligns with modern testing practices.


28-28: Use of JUnit 5 @test annotation.

The @Test annotation is now sourced from org.junit.jupiter.api.Test, which is appropriate for JUnit 5.


33-33: Visibility change for the test class.

The class TestMoneyParseContext has been changed from public to package-private. This is generally acceptable for test classes unless they need to be accessed from other packages.

Ensure that this change does not affect any external test runners or frameworks that might require public visibility.


Line range hint 36-143:
Improved assertion methods and exception testing.

The assertions have been updated to use assertNull, assertFalse, and assertThrows, which improve readability and align with JUnit 5 best practices. The method visibility has been changed to package-private, which is consistent with JUnit 5 conventions.

src/test/java/org/joda/money/TestMoneyUtils_BigMoney.java (5)

18-23: Update to JUnit 5 imports looks good.

The transition to JUnit 5 assertions is correctly implemented. This change enhances the test suite's compatibility with modern testing practices.


28-28: JUnit 5 @Test annotation is correctly used.

The import statement for @Test has been updated to JUnit 5, which is appropriate for the migration.


33-33: Visibility change to package-private is appropriate.

Changing the class visibility from public to package-private is a good practice for test classes, as they do not need to be exposed outside their package.


47-47: Test method visibility change is appropriate.

Changing the method visibility to package-private is consistent with JUnit 5 practices and helps encapsulate the test logic within the package.


123-126: Use of assertThrows for exception testing is correct.

The use of assertThrows enhances readability and aligns with JUnit 5's approach to exception testing.

src/test/java/org/joda/money/TestMoneyUtils_Money.java (5)

18-23: Update to JUnit 5 imports looks good.

The transition to JUnit 5 assertions is correctly implemented. This change enhances the test suite's compatibility with modern testing practices.


25-25: JUnit 5 @Test annotation is correctly used.

The import statement for @Test has been updated to JUnit 5, which is appropriate for the migration.


30-30: Visibility change to package-private is appropriate.

Changing the class visibility from public to package-private is a good practice for test classes, as they do not need to be exposed outside their package.


49-57: Use of assertThrows for exception testing is correct.

The use of assertThrows enhances readability and aligns with JUnit 5's approach to exception testing.


129-132: Use of assertThrows for exception testing is correct.

The use of assertThrows enhances readability and aligns with JUnit 5's approach to exception testing.

src/test/java/org/joda/money/format/TestMoneyFormatter.java (5)

18-22: Update to JUnit 5 imports looks good.

The transition to JUnit 5 assertions is correctly implemented. This change enhances the test suite's compatibility with modern testing practices.


37-41: Lifecycle and parameterized test annotations are correctly updated.

The use of @BeforeEach, @AfterEach, @ParameterizedTest, and @MethodSource aligns with JUnit 5's practices, enhancing test setup and flexibility.


46-46: Visibility change to package-private is appropriate.

Changing the class visibility from public to package-private is a good practice for test classes, as they do not need to be exposed outside their package.


58-58: Method visibility change to package-private is appropriate.

Changing the method visibility to package-private is consistent with JUnit 5 practices and helps encapsulate the test logic within the package.


125-127: Use of assertThrows for exception testing is correct.

The use of assertThrows enhances readability and aligns with JUnit 5's approach to exception testing.

src/test/java/org/joda/money/TestCurrencyUnit.java (2)

18-21: Imports updated to JUnit 5.

The import statements have been correctly updated to use JUnit 5 assertions and annotations.

Also applies to: 37-37


73-77: Use of assertThrows for exception testing.

The use of assertThrows is a good practice in JUnit 5 for testing exceptions. It improves readability and clarity.

pom.xml (1)

523-524: JUnit 5 dependency update.

The dependencies for JUnit have been correctly updated to JUnit 5.11.0. The removal of junit-dataprovider is consistent with moving to JUnit 5's built-in parameterized testing capabilities.

Also applies to: 789-789

src/test/java/org/joda/money/format/TestMoneyFormatterBuilder.java (3)

18-22: Imports updated to JUnit 5.

The import statements have been correctly updated to use JUnit 5 assertions, annotations, and parameterized test features.

Also applies to: 33-37


66-67: Lifecycle annotations updated to JUnit 5.

The @BeforeEach and @AfterEach annotations are correctly used to handle setup and teardown, replacing JUnit 4's @Before and @After.

Also applies to: 72-73


372-374: Transition to @ParameterizedTest.

The use of @ParameterizedTest with @MethodSource is a modern and flexible way to handle parameterized tests in JUnit 5.

Also applies to: 418-420, 619-621, 637-639, 655-657, 698-700

src/test/java/org/joda/money/format/TestMoneyAmountStyle.java (4)

18-23: Import changes approved.

The import statements have been correctly updated to use JUnit 5's org.junit.jupiter.api package. This is consistent with the migration objectives.

Also applies to: 30-32


37-51: Class and lifecycle method changes approved.

The class visibility and lifecycle annotations have been correctly updated to JUnit 5 standards. The transition from @Before and @After to @BeforeEach and @AfterEach is appropriate.


Line range hint 59-822:
Test method and assertion changes approved.

The test methods have been updated with JUnit 5 assertions, enhancing readability and maintainability. The change in method visibility to package-private is also consistent with JUnit 5 practices.


589-594: Exception handling change approved.

The use of assertThrows for testing exceptions is a correct and modern approach in JUnit 5.

@jodastephen jodastephen merged commit 983b382 into main Aug 17, 2024
4 checks passed
@jodastephen jodastephen deleted the junit branch August 17, 2024 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant