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

fix unit tests #540

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

fix unit tests #540

wants to merge 1 commit into from

Conversation

Just-CJ
Copy link

@Just-CJ Just-CJ commented Sep 14, 2024

Motivation:

修复单元测试,主要修复tracer-sofa-boot-starter模块下单元测试不能正常运行的问题。

Modification:

  1. 调整embedmongo-spring为可选的编译期依赖,修复源码编译问题、
  2. 添加com.alipay.sofa:sofa-boot依赖
    Support springcloud 3.1.x #512 该pr中移除了infra-sofa-boot-starter依赖,这导致运行时缺少了com.alipay.sofa.boot.listener.SofaBootstrapRunListener,从而导致SofaTracerConfigurationListener无法正常的进行参数注入,测试用例无法正常执行。
image

项目正常依赖的sofaboot版本中,SofaBootstrapRunListener已经迁移到了com.alipay.sofa:sofa-boot中,不再需要依赖infra-sofa-boot-starter,重新将该依赖补回即可。

Result:

Fixes #.

If there is no issue then describe the changes introduced by this PR.

Summary by CodeRabbit

  • New Features

    • Introduced a new dependency for sofa-boot, enhancing project functionality.
    • Added a new junit dependency to improve testing capabilities.
  • Bug Fixes

    • Updated mockito-core dependency to version 4.3.0, improving testing performance and reliability.
    • Removed specific version specifications for mockito-core in several plugins, allowing for the use of the latest version.
  • Refactor

    • Simplified the method for checking Spring Cloud bootstrap environment.
    • Changed AbstractTestCloudBase from a concrete to an abstract class to better serve as a base for other classes.
  • Chores

    • Removed unnecessary import statements to streamline code and reduce dependencies.
    • Commented out unused test methods to clean up the test suite.

Copy link

coderabbitai bot commented Sep 14, 2024

Walkthrough

The pull request encompasses several updates primarily focused on dependency management within various pom.xml files and adjustments to Java test classes. Key changes include upgrading the mockito-core dependency from version 1.10.19 to 4.3.0, removing specific version declarations for mockito-core in multiple plugin projects, and refactoring import statements and class declarations to align with the updated package structure and functionality.

Changes

File Change Summary
pom.xml Updated mockito-core version from 1.10.19 to 4.3.0; removed version declarations in plugins.
BaseTest.java Updated import from org.mockito.runners.MockitoJUnitRunner to org.mockito.junit.MockitoJUnitRunner.
CountBenchmark.java Removed @Test annotation, indicating a shift to JMH for benchmarking.
tracer-sofa-boot-starter/pom.xml Added sofa-boot dependency; modified de.flapdoodle.embed.mongo version; marked embedmongo-spring as optional; added junit dependency for testing.
SofaTracerConfigurationListener.java Refactored method to use SofaBootEnvUtils for environment checks.
AbstractTestBase.java Updated import for SofaBootstrapRunListener to new package.
AbstractTestCloudBase.java Changed from concrete to abstract class; updated import for SofaBootstrapRunListener.
ConfigurationHolderListener.java Renamed import from SOFABootEnvUtils to SofaBootEnvUtils.
SpringBootWebApplication.java Excluded EmbeddedMongoAutoConfiguration from @SpringBootApplication.
ConfigurationTest.java Removed imports for Spring testing framework components.
tracer-test/core-test/pom.xml Removed version specification for mockito-core dependency, changing from 2.3.0 to unspecified.

Possibly related PRs

Poem

🐇 In the garden of code, changes bloom bright,
Mockito's new version, a delightful sight.
Dependencies dance, old paths now align,
Spring's gentle whispers, in tests they entwine.
With each little tweak, our code takes a leap,
Hopping towards clarity, in logic we keep! 🌼


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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 and nitpick comments (2)
tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java (1)

Line range hint 34-40: Enhance class documentation for dependency requirements.

Given the critical role of this listener in early configuration and its dependency on sofa-boot, consider enhancing the class documentation to:

  1. Document the required sofa-boot dependency
  2. Explain the reasoning behind the execution order (HIGHEST_PRECEDENCE + 30)

Add the following to the class documentation:

 /**
  * Parse SOFATracer Configuration in early stage.
  *
+ * Requires com.alipay.sofa:sofa-boot dependency for environment utilities.
+ * Executes with high precedence to ensure tracer configuration is available
+ * before other components initialize.
  *
  * @author qilong.zql
  * @since 2.2.2
  */
pom.xml (1)

Line range hint 1-516: Missing core dependency change

According to the PR objectives, this change should add com.alipay.sofa:sofa-boot dependency to fix the unit tests, but this change is missing from the POM file.

Add the following dependency to the dependencyManagement section:

     <dependencyManagement>
         <dependencies>
+            <dependency>
+                <groupId>com.alipay.sofa</groupId>
+                <artifactId>sofa-boot</artifactId>
+                <version>${sofa.boot.version}</version>
+            </dependency>

Note: Please also define the sofa.boot.version property in the properties section with the appropriate version.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 915825f and 87a0017.

📒 Files selected for processing (14)
  • pom.xml (1 hunks)
  • sofa-tracer-plugins/sofa-tracer-datasource-plugin/pom.xml (0 hunks)
  • sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/test/java/com/sofa/tracer/plugins/datasource/base/BaseTest.java (1 hunks)
  • sofa-tracer-plugins/sofa-tracer-kafkamq-plugin/pom.xml (0 hunks)
  • sofa-tracer-plugins/sofa-tracer-rabbitmq-plugin/pom.xml (0 hunks)
  • tracer-core/src/test/java/com/alipay/common/tracer/core/benchmark/CountBenchmark.java (0 hunks)
  • tracer-sofa-boot-starter/pom.xml (2 hunks)
  • tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java (2 hunks)
  • tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/AbstractTestBase.java (1 hunks)
  • tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/AbstractTestCloudBase.java (2 hunks)
  • tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/ConfigurationHolderListener.java (2 hunks)
  • tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/SpringBootWebApplication.java (2 hunks)
  • tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/config/ConfigurationTest.java (0 hunks)
  • tracer-test/core-test/pom.xml (0 hunks)
💤 Files with no reviewable changes (6)
  • sofa-tracer-plugins/sofa-tracer-datasource-plugin/pom.xml
  • sofa-tracer-plugins/sofa-tracer-kafkamq-plugin/pom.xml
  • sofa-tracer-plugins/sofa-tracer-rabbitmq-plugin/pom.xml
  • tracer-core/src/test/java/com/alipay/common/tracer/core/benchmark/CountBenchmark.java
  • tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/config/ConfigurationTest.java
  • tracer-test/core-test/pom.xml
✅ Files skipped from review due to trivial changes (2)
  • sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/test/java/com/sofa/tracer/plugins/datasource/base/BaseTest.java
  • tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/AbstractTestBase.java
🔇 Additional comments (12)
tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/SpringBootWebApplication.java (2)

20-20: LGTM!

The import statement for EmbeddedMongoAutoConfiguration is correctly added to support the configuration exclusion.


32-36: LGTM! Good practice to exclude unnecessary auto-configuration.

The exclusion of EmbeddedMongoAutoConfiguration aligns well with the PR objective of making the embedmongo-spring dependency optional. This prevents the auto-configuration from attempting to start an embedded MongoDB instance when it's not needed for the tests, potentially improving test execution time and reducing resource usage.

tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/ConfigurationHolderListener.java (2)

36-36: LGTM! Method usage correctly updated.

The SofaBootEnvUtils.isSpringCloudBootstrapEnvironment() call has been properly updated to use the new utility class while maintaining the same functionality.


21-21: LGTM! Verify complete migration of utility classes.

The import change aligns with the PR objective of migrating from infra-sofa-boot-starter to sofa-boot.

Let's verify no other references to the old package exist:

✅ Verification successful

Migration to SofaBootEnvUtils is complete and consistent

The verification confirms:

  • No references to the old sofa.infra.utils.SOFABootEnvUtils package exist
  • The new sofa.boot.util.SofaBootEnvUtils is properly used in both test and main code
  • No remaining references to infra-sofa-boot-starter in build files
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining references to the old SOFABootEnvUtils package
# Expect: No results, indicating complete migration

rg "com\.alipay\.sofa\.infra\.utils\.SOFABootEnvUtils" --type java

Length of output: 68


Script:

#!/bin/bash
# Let's verify with a broader search to ensure complete migration
# 1. Check for any references to old package
rg "sofa\.infra\.utils\.SOFABootEnvUtils" --type java

# 2. Check current usage of SofaBootEnvUtils to confirm proper migration
rg "sofa\.boot\.util\.SofaBootEnvUtils" --type java

# 3. Check for any remaining references to infra-sofa-boot-starter in build files
fd "pom.xml|build.gradle" --exec grep -l "infra-sofa-boot-starter" {}

Length of output: 501

tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/AbstractTestCloudBase.java (2)

44-44: LGTM! Good practice making the base test class abstract.

Making AbstractTestCloudBase abstract is a good practice as:

  1. It enforces the class's role as a base test class
  2. Prevents direct instantiation
  3. Matches the "Abstract" prefix in the class name

21-21: Verify the SofaBootstrapRunListener import change.

The import statement has been updated to use the new package location com.alipay.sofa.boot.listener, which aligns with the PR objective of switching to the sofa-boot dependency where this class was migrated.

Let's verify the class exists in the new location:

✅ Verification successful

Let me try a different search strategy to verify the import change.


Let me try one final verification to check the dependency in the module's pom.xml specifically.


Import change verified and confirmed correct

The verification confirms:

  • The sofa-boot dependency is correctly added in the module's pom.xml
  • The new import com.alipay.sofa.boot.listener.SofaBootstrapRunListener is used consistently in test files
  • No references to the old package com.alipay.sofa.infra.listener exist in the codebase
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify SofaBootstrapRunListener exists in the new package
# and is not present in the old package

echo "Checking new package location..."
ast-grep --pattern 'package com.alipay.sofa.boot.listener;
$$$
class SofaBootstrapRunListener {
  $$$
}'

echo "Checking old package location (should not exist)..."
ast-grep --pattern 'package com.alipay.sofa.infra.listener;
$$$
class SofaBootstrapRunListener {
  $$$
}'

Length of output: 402


Script:

#!/bin/bash
# Search for any file containing SofaBootstrapRunListener class definition
echo "Searching for SofaBootstrapRunListener class definition..."
rg "class SofaBootstrapRunListener" -A 2

# Search for any imports of SofaBootstrapRunListener
echo -e "\nSearching for imports of SofaBootstrapRunListener..."
rg "import.*SofaBootstrapRunListener"

# Check dependencies in pom.xml
echo -e "\nChecking sofa-boot dependency in pom.xml..."
rg -A 3 "sofa-boot" "pom.xml"

Length of output: 970


Script:

#!/bin/bash
# Check dependencies in the module's pom.xml
echo "Checking dependencies in tracer-sofa-boot-starter/pom.xml..."
cat tracer-sofa-boot-starter/pom.xml | grep -A 5 -B 5 "sofa-boot"

# Also check if there are any references to the old package
echo -e "\nChecking for any references to old package..."
rg "com.alipay.sofa.infra.listener"

Length of output: 1010

tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java (2)

21-21: LGTM: Import change aligns with dependency updates.

The addition of SofaBootEnvUtils import is consistent with the PR's objective to utilize functionality from the newly added sofa-boot dependency.


48-50: Verify behavior consistency with SofaBootEnvUtils.

The change to use SofaBootEnvUtils.isSpringCloudBootstrapEnvironment improves code maintainability by centralizing the environment check logic. However, we should verify that the behavior remains consistent with the previous implementation.

tracer-sofa-boot-starter/pom.xml (3)

20-23: LGTM: Addition of sofa-boot dependency.

This addition aligns with the PR objectives and is necessary to restore the SofaBootstrapRunListener functionality, which was previously available through infra-sofa-boot-starter.


272-273: LGTM: Making embedmongo-spring optional.

Making this dependency optional is appropriate as it helps fix the compilation issues mentioned in the PR objectives and prevents unnecessary transitive dependencies for projects that don't need MongoDB testing capabilities.


275-278: Consider removing explicit junit dependency.

The junit dependency is already included transitively through spring-boot-starter-test. Unless you need a specific version of JUnit, this explicit declaration might be redundant.

Let's verify the transitive dependency:

pom.xml (1)

258-258: Verify compatibility with Mockito 4.3.0

The upgrade from Mockito 1.10.19 to 4.3.0 is a significant version jump that may introduce breaking changes in test code. Please ensure:

  1. All test classes are compatible with Mockito 4.x API
  2. No deprecated methods are in use
✅ Verification successful

Mockito 4.3.0 upgrade appears safe to proceed

Based on the analysis of Mockito usage across the test files:

  1. No usage of deprecated Matchers class (replaced by ArgumentMatchers in Mockito 2.x+)
  2. No usage of deprecated MockitoJUnitRunner class
  3. No usage of RETURNS_DEEP_STUBS which had behavior changes
  4. All mock usage follows modern patterns compatible with Mockito 4.x:
    • Standard mock() method calls
    • Modern when() and verify() syntax
    • Proper use of argument matchers
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for common Mockito API usage that might need updates
echo "Searching for potential compatibility issues..."

# Check for RETURNS_DEEP_STUBS usage (behavior changed in 2.x)
rg "RETURNS_DEEP_STUBS" -l

# Check for common deprecated methods
rg "org\.mockito\.Matchers" -l  # replaced with org.mockito.ArgumentMatchers
rg "org\.mockito\.runners\." -l # replaced with org.mockito.junit.MockitoJUnitRunner

# Check mock() method usage (syntax changed in 2.x)
rg "mock\((.*?),.*?withSettings\(\)" -l

Length of output: 240


Script:

#!/bin/bash
# Let's try a broader search for Mockito usage patterns to better assess compatibility
# Look for Mockito imports and common patterns with context
echo "=== Searching for Mockito usage patterns ==="
rg "import.*org\.mockito" -l

echo -e "\n=== Checking test files with Mockito annotations ==="
rg "@Mock|@InjectMocks|@Spy" -l

echo -e "\n=== Looking for specific Mockito method calls ==="
# Common methods that might need updates between versions
rg "verify\(|when\(|mock\(" -A 2

echo -e "\n=== Checking for test base classes and runners ==="
rg "extends.*Test|@RunWith" -l

Length of output: 42752

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant