hivemq-community-edition icon indicating copy to clipboard operation
hivemq-community-edition copied to clipboard

Replace Deprecated MockitoAnnotations.initMocks

Open zjdaniels1985 opened this issue 1 year ago • 2 comments
trafficstars

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();
    }
...

zjdaniels1985 avatar Oct 25 '24 04:10 zjdaniels1985