Replace deprecated 'initMocks' calls with MockitoSession

Since the 'MockitoAnnotations.initMocks' method used to initialise
@Mock, @Spy, etc. fields is deprecated, use MockitoSession instead, as
suggested by the Javadoc. (Or just remove the call, if the test class is
not actually using any Mockito annotations.)

This allows strict stubbing (the default for Mockito 4), though it must
be configured to be lenient in the P2P test classes, to prevent failures
due to unused stubs.
This commit is contained in:
Steven Barclay 2023-11-28 03:36:25 +08:00
parent 4c0c11bb27
commit 32d4a8f50d
No known key found for this signature in database
GPG Key ID: 9FED6BF1176D500B
5 changed files with 50 additions and 27 deletions

View File

@ -52,9 +52,10 @@ import java.util.stream.IntStream;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoSession;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -91,6 +92,7 @@ public class BurningManServiceTest {
@Nested @Nested
public class BurnShareTest { public class BurnShareTest {
private MockitoSession mockitoSession;
@Mock @Mock
private DaoStateService daoStateService; private DaoStateService daoStateService;
@Mock @Mock
@ -102,13 +104,18 @@ public class BurningManServiceTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); mockitoSession = Mockito.mockitoSession().initMocks(this).startMocking();
when(cyclesInDaoStateService.getChainHeightOfPastCycle(800000, BurningManService.NUM_CYCLES_BURN_AMOUNT_DECAY)) when(cyclesInDaoStateService.getChainHeightOfPastCycle(800000, BurningManService.NUM_CYCLES_BURN_AMOUNT_DECAY))
.thenReturn(750000); .thenReturn(750000);
when(cyclesInDaoStateService.getChainHeightOfPastCycle(800000, BurningManService.NUM_CYCLES_COMP_REQUEST_DECAY)) when(cyclesInDaoStateService.getChainHeightOfPastCycle(800000, BurningManService.NUM_CYCLES_COMP_REQUEST_DECAY))
.thenReturn(700000); .thenReturn(700000);
} }
@AfterEach
public void tearDown() {
mockitoSession.finishMocking();
}
private void addProofOfBurnTxs(Tx... txs) { private void addProofOfBurnTxs(Tx... txs) {
var txsById = Arrays.stream(txs) var txsById = Arrays.stream(txs)
.collect(Collectors.toMap(Tx::getId, tx -> tx)); .collect(Collectors.toMap(Tx::getId, tx -> tx));

View File

@ -36,8 +36,11 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.Mockito;
import org.mockito.MockitoSession;
import org.mockito.quality.Strictness;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -48,20 +51,22 @@ import static org.mockito.Mockito.*;
* Tests of the P2PDataStorage::onRemoved callback behavior to ensure that the proper number of signal events occur. * Tests of the P2PDataStorage::onRemoved callback behavior to ensure that the proper number of signal events occur.
*/ */
public class ProposalServiceP2PDataStorageListenerTest { public class ProposalServiceP2PDataStorageListenerTest {
private ProposalService proposalService; private MockitoSession mockitoSession;
private ProposalService proposalService;
@Mock @Mock
private PeriodService periodService; private PeriodService periodService;
@Mock @Mock
private DaoStateService daoStateService; private DaoStateService daoStateService;
@Mock @Mock
private ListChangeListener<Proposal> tempProposalListener; private ListChangeListener<Proposal> tempProposalListener;
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); mockitoSession = Mockito.mockitoSession()
.initMocks(this)
.strictness(Strictness.LENIENT) // the two stubs below are not used in every test
.startMocking();
this.proposalService = new ProposalService( this.proposalService = new ProposalService(
mock(P2PService.class), mock(P2PService.class),
@ -78,6 +83,11 @@ public class ProposalServiceP2PDataStorageListenerTest {
when(this.daoStateService.isParseBlockChainComplete()).thenReturn(false); when(this.daoStateService.isParseBlockChainComplete()).thenReturn(false);
} }
@AfterEach
public void tearDown() {
mockitoSession.finishMocking();
}
private static ProtectedStorageEntry buildProtectedStorageEntry() { private static ProtectedStorageEntry buildProtectedStorageEntry() {
ProtectedStorageEntry protectedStorageEntry = mock(ProtectedStorageEntry.class); ProtectedStorageEntry protectedStorageEntry = mock(ProtectedStorageEntry.class);
TempProposalPayload tempProposalPayload = mock(TempProposalPayload.class); TempProposalPayload tempProposalPayload = mock(TempProposalPayload.class);

View File

@ -45,8 +45,11 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.Mockito;
import org.mockito.MockitoSession;
import org.mockito.quality.Strictness;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -60,6 +63,7 @@ import static org.mockito.Mockito.withSettings;
public class P2PDataStorageBuildGetDataResponseTest { public class P2PDataStorageBuildGetDataResponseTest {
abstract static class P2PDataStorageBuildGetDataResponseTestBase { abstract static class P2PDataStorageBuildGetDataResponseTestBase {
private MockitoSession mockitoSession;
// GIVEN null & non-null supportedCapabilities // GIVEN null & non-null supportedCapabilities
private TestState testState; private TestState testState;
@ -72,7 +76,10 @@ public class P2PDataStorageBuildGetDataResponseTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); mockitoSession = Mockito.mockitoSession()
.initMocks(this)
.strictness(Strictness.LENIENT) // there are unused stubs in TestState & elsewhere
.startMocking();
this.testState = new TestState(); this.testState = new TestState();
this.localNodeAddress = new NodeAddress("localhost", 8080); this.localNodeAddress = new NodeAddress("localhost", 8080);
@ -82,6 +89,11 @@ public class P2PDataStorageBuildGetDataResponseTest {
Capabilities.app.addAll(Capability.MEDIATION); Capabilities.app.addAll(Capability.MEDIATION);
} }
@AfterEach
public void tearDown() {
mockitoSession.finishMocking();
}
static class RequiredCapabilitiesPNPStub extends PersistableNetworkPayloadStub static class RequiredCapabilitiesPNPStub extends PersistableNetworkPayloadStub
implements CapabilityRequiringPayload { implements CapabilityRequiringPayload {
Capabilities capabilities; Capabilities capabilities;

View File

@ -34,8 +34,6 @@ import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import org.mockito.MockitoAnnotations;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -50,7 +48,6 @@ public class P2PDataStorageProcessGetDataResponse {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this);
this.testState = new TestState(); this.testState = new TestState();
this.peerNodeAddress = new NodeAddress("peer", 8080); this.peerNodeAddress = new NodeAddress("peer", 8080);

View File

@ -35,8 +35,6 @@ import java.security.NoSuchAlgorithmException;
import java.util.Set; import java.util.Set;
import org.mockito.MockitoAnnotations;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -53,7 +51,6 @@ public class P2PDataStorageRequestDataTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this);
this.testState = new TestState(); this.testState = new TestState();
this.localNodeAddress = new NodeAddress("localhost", 8080); this.localNodeAddress = new NodeAddress("localhost", 8080);