Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update Mockito to 5.16.1 #10686

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

Merged
merged 1 commit into from
Apr 16, 2025
Merged

Update Mockito to 5.16.1 #10686

merged 1 commit into from
Apr 16, 2025

Conversation

erickalendra
Copy link
Contributor

Description

This PR updates Mockito to 5.16.1 from 4.
See the release notes for high level changes: https://github.com/mockito/mockito/releases/tag/v5.0.0

Dependency name change mockito-inline to mockito-core. Inline is now the default and the last version of mockito-inline released is 5.2.0.

  • assertj-core in user-authenticators/saml2 pulls in an incompatible version of byte-buddy and required an exclusion. Updating the version of assertj is left for a future PR.

The upgrade requires Java 11+, dropping support for Java 8. CloudStack documentation already says to use Java 11 and does not indicate that java 8 is supported.

Test classes using @RunWith(MockitoJUnitRunner.class) now get run in strict mode. Changes were made to tests where the stubbing intention was clear. In ManagementServerMaintenanceManagerImplTest there are 5 tests where the intention of the test is unclear. Each of the statements now use Mockito.lenient() to avoid the exception. Other cases in the tests follow a similar pattern.

Unnecessary stubbings detected in test class: ManagementServerMaintenanceManagerImplTest
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
  1. -> at org.apache.cloudstack.maintenance.ManagementServerMaintenanceManagerImplTest.prepareForShutdownCmdFailedResponseFromClusterManager(ManagementServerMaintenanceManagerImplTest.java:189)
  2. -> at org.apache.cloudstack.maintenance.ManagementServerMaintenanceManagerImplTest.prepareForShutdownCmdNullResponseFromClusterManager(ManagementServerMaintenanceManagerImplTest.java:173)
  3. -> at org.apache.cloudstack.maintenance.ManagementServerMaintenanceManagerImplTest.prepareForShutdownCmdSuccessResponseFromClusterManager(ManagementServerMaintenanceManagerImplTest.java:204)
  4. -> at org.apache.cloudstack.maintenance.ManagementServerMaintenanceManagerImplTest.prepareForShutdownCmdOtherMsHostsInPreparingState(ManagementServerMaintenanceManagerImplTest.java:158)
  5. -> at org.apache.cloudstack.maintenance.ManagementServerMaintenanceManagerImplTest.triggerShutdownCmdMsInUpStateAndOtherMsHostsInPreparingState(ManagementServerMaintenanceManagerImplTest.java:283)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.

Minor clean up.

  • Both @Spy and Mockito.spy( should not be used. Favored the annotation.
  • Both @RunWith(MockitoJUnitRunner.class) and MockitoAnnotations.openMocks(this); should not be used. Favored the annotation.
  • Unnecessary extends TestCase removed.
  • @InjectMocks and new in statement unnecessary. Removed new when issue presented.
  • Some of the Cmd classes like UpdateNetworkCmd have a type tree that includes fields of type Object. This appears to cause issues with injection, requiring that @Mock fields be available. This is where the following fields were added in multiple places:
    • Object job;
    • ResponseGenerator _responseGenerator;
  • Wrong number of parameters for Mockito.when in LibvirtRevertSnapshotCommandWrapperTest.java

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • build/CI
  • test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

All tests that run with flags: -Dnoredist -P developer,systemvm.

Commands used:
mvn clean install -Dnoredist -P developer,systemvm

Do not need to verify running of the server or agent because Mockito is only used in tests.

How did you try to break this feature and the system with this change?

Dependency name change mockito-inline to mockito-core. Inline is now the default and the last version of mockito-inline released is 5.2.0.

assertj-core in user-authenticators/saml2 pulls in an incompatible version of byte-buddy and required an exclusion. Updating the version of assertj is left for a future PR.
The upgrade requires Java 11+, dropping support for Java 8. CloudStack documentation already says to use Java 11 and does not indicate that java 8 is supported.

Test classes using @RunWith(MockitoJUnitRunner.class) now get run in strict mode. Changes were made to tests where the stubbing intention was clear. In ManagementServerMaintenanceManagerImplTest there are 5 tests where the intention of the test is unclear. Each of the statements now use Mockito.lenient() to avoid the exception. Other cases in the tests follow a similar pattern.
Minor clean up.

Both @SPY and Mockito.spy( should not be used. Favored the annotation.
Both @RunWith(MockitoJUnitRunner.class) and MockitoAnnotations.openMocks(this); should not be used. Favored the annotation.
Unnecessary extends TestCase removed.
@Injectmocks and new in statement unnecessary. Removed new when issue presented.
Some of the Cmd classes like UpdateNetworkCmd have a type tree that includes fields of type Object. This appears to cause issues with injection, requiring that @mock fields be available. This is where the following fields were added in multiple places:
Object job;
ResponseGenerator _responseGenerator;
Wrong number of parameters for Mockito.when in LibvirtRevertSnapshotCommandWrapperTest.java
Copy link

boring-cyborg bot commented Apr 9, 2025

Congratulations on your first Pull Request and welcome to the Apache CloudStack community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/cloudstack/blob/main/CONTRIBUTING.md)
Here are some useful points:

@DaanHoogland
Copy link
Contributor

@blueorangutan package

@blueorangutan
Copy link

@DaanHoogland a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.

@vishesh92 vishesh92 requested a review from Copilot April 10, 2025 08:35
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 20 out of 24 changed files in this pull request and generated 1 comment.

Files not reviewed (4)
  • plugins/storage/volume/storpool/pom.xml: Language not supported
  • plugins/user-authenticators/ldap/pom.xml: Language not supported
  • plugins/user-authenticators/saml2/pom.xml: Language not supported
  • pom.xml: Language not supported
Comments suppressed due to low confidence (2)

server/src/test/java/com/cloud/network/element/VirtualRouterElementTest.java:174

  • [nitpick] Consider using a consistent naming style for mocks. Since other mocks in this file use an underscore prefix (e.g. _vpnDao), renaming loadBalancerDao to _loadBalancerDao could improve readability.
@Mock private LoadBalancerDao loadBalancerDao;

server/src/test/java/org/apache/cloud/api/query/QueryManagerImplTest.java:116

  • The QueryManagerImpl spy instance is no longer initialized, which could lead to a NullPointerException. Consider instantiating it explicitly (e.g. new QueryManagerImpl()) or ensure it is initialized by the test framework.
private QueryManagerImpl queryManagerImplSpy;

Comment on lines +707 to 708
LibvirtMigrateCommandWrapper spyLibvirtMigrateCmdWrapper = libvirtMigrateCmdWrapper;
Mockito.doNothing().when(spyLibvirtMigrateCmdWrapper).deleteLocalVolume("volPath");
Copy link
Preview

Copilot AI Apr 10, 2025

Choose a reason for hiding this comment

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

[nitpick] The redundant spy wrapping is unnecessary since libvirtMigrateCmdWrapper is already a spy. Removing this redundant wrapping can simplify the test code.

Suggested change
LibvirtMigrateCommandWrapper spyLibvirtMigrateCmdWrapper = libvirtMigrateCmdWrapper;
Mockito.doNothing().when(spyLibvirtMigrateCmdWrapper).deleteLocalVolume("volPath");
Mockito.doNothing().when(libvirtMigrateCmdWrapper).deleteLocalVolume("volPath");

Copilot uses AI. Check for mistakes.


@Before
public void setup() throws Exception {
closeable = MockitoAnnotations.openMocks(this);
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The class is annotated with @RunWith(MockitoJUnitRunner.class) which is calling MockitoAnnotations.openMocks(...); before every test. Calling openMocks a second time can have undesirable side effects.

I removed the line as it was unnecessary and could issues in some circumstances.

@@ -154,7 +154,7 @@ public void prepareForShutdownCmdOtherMsHostsInPreparingState() {
ManagementServerHostVO msHost2 = mock(ManagementServerHostVO.class);
List<ManagementServerHostVO> msHostList = new ArrayList<>();
msHostList.add(msHost2);
Mockito.when(msHostDao.listBy(any())).thenReturn(msHostList);
Mockito.lenient().when(msHostDao.listBy(any())).thenReturn(msHostList);
Copy link
Member

@vishesh92 vishesh92 Apr 10, 2025

Choose a reason for hiding this comment

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

iirc mockito was throwing errors earlier when an unnecessary mocks was setup. Why is lenient() required now? Can't we just remove the mock if it's not required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can be removed. I choose not to remove it because I did not want to significantly alter any logic of the tests as part of the Mockito version upgrade.

It is unclear to me if the mock is not being used by accident and it should be used. I feel the discussion about the test intention should happen independently of the Mockito upgrade.

Copy link

codecov bot commented Apr 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 17.17%. Comparing base (4c31f9d) to head (6bc4c5c).
Report is 7 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main   #10686      +/-   ##
============================================
+ Coverage     16.30%   17.17%   +0.86%     
+ Complexity    13449    13445       -4     
============================================
  Files          5676     5277     -399     
  Lines        499208   466608   -32600     
  Branches      60374    54592    -5782     
============================================
- Hits          81414    80126    -1288     
+ Misses       408722   377562   -31160     
+ Partials       9072     8920     -152     
Flag Coverage Δ
uitests ?
unittests 17.17% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@blueorangutan
Copy link

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ debian ✔️ suse15. SL-JID 13006

@DaanHoogland
Copy link
Contributor

@vishesh92 , this seems ok to me as it is only unit tests I do not agree with copilot about the '_'s so I say let's merge. Any objections?

@rohityadavcloud
Copy link
Member

@blueorangutan test

@blueorangutan
Copy link

@rohityadavcloud a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

@vishesh92
Copy link
Member

@vishesh92 , this seems ok to me as it is only unit tests I do not agree with copilot about the '_'s so I say let's merge. Any objections?

yeah. Unit tests are passing, so not much of a concern here.

Copy link
Member

@weizhouapache weizhouapache left a comment

Choose a reason for hiding this comment

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

overall code lgtm

@vishesh92 vishesh92 self-requested a review April 10, 2025 12:19
Copy link
Member

@vishesh92 vishesh92 left a comment

Choose a reason for hiding this comment

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

clgtm
And tests are passing.

@andrijapanicsb
Copy link
Contributor

(test env has been deleted due to space constrains, pls rekick tomorrow again)

@weizhouapache
Copy link
Member

@blueorangutan package

@blueorangutan
Copy link

@weizhouapache a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.

@blueorangutan
Copy link

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ debian ✔️ suse15. SL-JID 13042

@weizhouapache
Copy link
Member

@blueorangutan test

@blueorangutan
Copy link

@weizhouapache a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

@blueorangutan
Copy link

[SF] Trillian test result (tid-12979)
Environment: kvm-ol8 (x2), Advanced Networking with Mgmt server ol8
Total time taken: 52840 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr10686-t12979-kvm-ol8.zip
Smoke tests completed. 141 look OK, 0 have errors, 0 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File

@rohityadavcloud rohityadavcloud added this to the 4.21.0 milestone Apr 16, 2025
Copy link
Member

@rohityadavcloud rohityadavcloud left a comment

Choose a reason for hiding this comment

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

LGTM

@rohityadavcloud
Copy link
Member

Merging this based on code reviews lgtms, GA/simulator tests and smoketests.

@rohityadavcloud rohityadavcloud merged commit 55c8115 into apache:main Apr 16, 2025
25 checks passed
Copy link

boring-cyborg bot commented Apr 16, 2025

Awesome work, congrats on your first merged pull request!

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.

8 participants