Skip to content

Replace Deprecated MockitoAnnotations.initMocks#542

Closed
zjdaniels1985 wants to merge 2 commits intohivemq:masterfrom
zjdaniels1985:update_deprecated_mockitoannotation_initMocks
Closed

Replace Deprecated MockitoAnnotations.initMocks#542
zjdaniels1985 wants to merge 2 commits intohivemq:masterfrom
zjdaniels1985:update_deprecated_mockitoannotation_initMocks

Conversation

@zjdaniels1985
Copy link
Copy Markdown

Replace the deprecated MockitoAnnotations.initMocks() method with the recommended Mockito.Annotations.openMocks() method

Motivation

  • initMocks was used to initialize mocks annotated with @mock, @SPY, etc., in your test class
  • openMocks is the recommended replacement since Mockito 3. It provides the same functionality but with better resource management
  • openMocks returns an AutoCloseable object, which ensures that resources are properly closed after the test execution

Changes

Old:

import org.mockito.MockitoAnnotations;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
}

New:

import org.mockito.MockitoAnnotations;

public class <TEST-CLASS-NAME> {

private AutoCloseable closeable;

@Before
public void setUp() {
    closeable = MockitoAnnotations.openMocks(this)) {
        ...
    }
}

@After
public void releaseMocks() throws Exception {
        closeable. close();
    }
...

@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Oct 25, 2024

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have the users @zjdaniels1985 on file. In order for us to review and merge your code, please sign our Contributor License Agreement to get yourself added. You'll find the CLA and more information here: https://github.com/hivemq/hivemq-community/blob/master/CONTRIBUTING.adoc#contributor-license-agreement

@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Oct 28, 2024

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have the users @zjdaniels1985 on file. In order for us to review and merge your code, please sign our Contributor License Agreement to get yourself added. You'll find the CLA and more information here: https://github.com/hivemq/hivemq-community/blob/master/CONTRIBUTING.adoc#contributor-license-agreement

@Donnerbart
Copy link
Copy Markdown
Contributor

Fixed in the recent commits (getting completely rid of the explicit mock initialization).

@Donnerbart Donnerbart closed this Feb 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants