hivemq-community-edition
hivemq-community-edition copied to clipboard
Replace Deprecated MockitoAnnotations.initMocks
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();
}
...