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);
@ -111,7 +108,7 @@ public class P2PDataStorageProcessGetDataResponse {
// XXXBUGXXX: We signal listeners w/ non ProcessOncePersistableNetworkPayloads // XXXBUGXXX: We signal listeners w/ non ProcessOncePersistableNetworkPayloads
@Test @Test
public void processGetDataResponse_newPNPUpdatesState() { public void processGetDataResponse_newPNPUpdatesState() {
PersistableNetworkPayload persistableNetworkPayload = new PersistableNetworkPayloadStub(new byte[] { 1 }); PersistableNetworkPayload persistableNetworkPayload = new PersistableNetworkPayloadStub(new byte[]{1});
GetDataResponse getDataResponse = buildGetDataResponse(persistableNetworkPayload); GetDataResponse getDataResponse = buildGetDataResponse(persistableNetworkPayload);
@ -137,7 +134,7 @@ public class P2PDataStorageProcessGetDataResponse {
// TESTCASE: GetDataResponse w/ existing PNP changes no state // TESTCASE: GetDataResponse w/ existing PNP changes no state
@Test @Test
public void processGetDataResponse_duplicatePNPDoesNothing() { public void processGetDataResponse_duplicatePNPDoesNothing() {
PersistableNetworkPayload persistableNetworkPayload = new PersistableNetworkPayloadStub(new byte[] { 1 }); PersistableNetworkPayload persistableNetworkPayload = new PersistableNetworkPayloadStub(new byte[]{1});
this.testState.mockedStorage.addPersistableNetworkPayload(persistableNetworkPayload, this.testState.mockedStorage.addPersistableNetworkPayload(persistableNetworkPayload,
this.peerNodeAddress, false); this.peerNodeAddress, false);
@ -152,7 +149,7 @@ public class P2PDataStorageProcessGetDataResponse {
// TESTCASE: GetDataResponse w/ missing PNP is added with no broadcast or listener signal (ProcessOncePersistableNetworkPayload) // TESTCASE: GetDataResponse w/ missing PNP is added with no broadcast or listener signal (ProcessOncePersistableNetworkPayload)
@Test @Test
public void processGetDataResponse_newPNPUpdatesState_LazyProcessed() { public void processGetDataResponse_newPNPUpdatesState_LazyProcessed() {
PersistableNetworkPayload persistableNetworkPayload = new LazyPersistableNetworkPayloadStub(new byte[] { 1 }); PersistableNetworkPayload persistableNetworkPayload = new LazyPersistableNetworkPayloadStub(new byte[]{1});
GetDataResponse getDataResponse = buildGetDataResponse(persistableNetworkPayload); GetDataResponse getDataResponse = buildGetDataResponse(persistableNetworkPayload);
@ -165,7 +162,7 @@ public class P2PDataStorageProcessGetDataResponse {
// TESTCASE: GetDataResponse w/ existing PNP changes no state (ProcessOncePersistableNetworkPayload) // TESTCASE: GetDataResponse w/ existing PNP changes no state (ProcessOncePersistableNetworkPayload)
@Test @Test
public void processGetDataResponse_duplicatePNPDoesNothing_LazyProcessed() { public void processGetDataResponse_duplicatePNPDoesNothing_LazyProcessed() {
PersistableNetworkPayload persistableNetworkPayload = new LazyPersistableNetworkPayloadStub(new byte[] { 1 }); PersistableNetworkPayload persistableNetworkPayload = new LazyPersistableNetworkPayloadStub(new byte[]{1});
this.testState.mockedStorage.addPersistableNetworkPayload(persistableNetworkPayload, this.testState.mockedStorage.addPersistableNetworkPayload(persistableNetworkPayload,
this.peerNodeAddress, false); this.peerNodeAddress, false);
@ -180,7 +177,7 @@ public class P2PDataStorageProcessGetDataResponse {
// TESTCASE: Second call to processGetDataResponse adds PNP for non-ProcessOncePersistableNetworkPayloads // TESTCASE: Second call to processGetDataResponse adds PNP for non-ProcessOncePersistableNetworkPayloads
@Test @Test
public void processGetDataResponse_secondProcessNewPNPUpdatesState() { public void processGetDataResponse_secondProcessNewPNPUpdatesState() {
PersistableNetworkPayload addFromFirstProcess = new PersistableNetworkPayloadStub(new byte[] { 1 }); PersistableNetworkPayload addFromFirstProcess = new PersistableNetworkPayloadStub(new byte[]{1});
GetDataResponse getDataResponse = buildGetDataResponse(addFromFirstProcess); GetDataResponse getDataResponse = buildGetDataResponse(addFromFirstProcess);
TestState.SavedTestState beforeState = this.testState.saveTestState(addFromFirstProcess); TestState.SavedTestState beforeState = this.testState.saveTestState(addFromFirstProcess);
@ -188,7 +185,7 @@ public class P2PDataStorageProcessGetDataResponse {
this.testState.verifyPersistableAdd( this.testState.verifyPersistableAdd(
beforeState, addFromFirstProcess, true, true, false); beforeState, addFromFirstProcess, true, true, false);
PersistableNetworkPayload addFromSecondProcess = new PersistableNetworkPayloadStub(new byte[] { 2 }); PersistableNetworkPayload addFromSecondProcess = new PersistableNetworkPayloadStub(new byte[]{2});
getDataResponse = buildGetDataResponse(addFromSecondProcess); getDataResponse = buildGetDataResponse(addFromSecondProcess);
beforeState = this.testState.saveTestState(addFromSecondProcess); beforeState = this.testState.saveTestState(addFromSecondProcess);
this.testState.mockedStorage.processGetDataResponse(getDataResponse, this.peerNodeAddress); this.testState.mockedStorage.processGetDataResponse(getDataResponse, this.peerNodeAddress);
@ -199,7 +196,7 @@ public class P2PDataStorageProcessGetDataResponse {
// TESTCASE: Second call to processGetDataResponse does not add any PNP (LazyProcessed) // TESTCASE: Second call to processGetDataResponse does not add any PNP (LazyProcessed)
@Test @Test
public void processGetDataResponse_secondProcessNoPNPUpdates_LazyProcessed() { public void processGetDataResponse_secondProcessNoPNPUpdates_LazyProcessed() {
PersistableNetworkPayload addFromFirstProcess = new LazyPersistableNetworkPayloadStub(new byte[] { 1 }); PersistableNetworkPayload addFromFirstProcess = new LazyPersistableNetworkPayloadStub(new byte[]{1});
GetDataResponse getDataResponse = buildGetDataResponse(addFromFirstProcess); GetDataResponse getDataResponse = buildGetDataResponse(addFromFirstProcess);
TestState.SavedTestState beforeState = this.testState.saveTestState(addFromFirstProcess); TestState.SavedTestState beforeState = this.testState.saveTestState(addFromFirstProcess);
@ -207,7 +204,7 @@ public class P2PDataStorageProcessGetDataResponse {
this.testState.verifyPersistableAdd( this.testState.verifyPersistableAdd(
beforeState, addFromFirstProcess, true, false, false); beforeState, addFromFirstProcess, true, false, false);
PersistableNetworkPayload addFromSecondProcess = new LazyPersistableNetworkPayloadStub(new byte[] { 2 }); PersistableNetworkPayload addFromSecondProcess = new LazyPersistableNetworkPayloadStub(new byte[]{2});
getDataResponse = buildGetDataResponse(addFromSecondProcess); getDataResponse = buildGetDataResponse(addFromSecondProcess);
beforeState = this.testState.saveTestState(addFromSecondProcess); beforeState = this.testState.saveTestState(addFromSecondProcess);
this.testState.mockedStorage.processGetDataResponse(getDataResponse, this.peerNodeAddress); this.testState.mockedStorage.processGetDataResponse(getDataResponse, this.peerNodeAddress);

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);
@ -119,8 +116,8 @@ public class P2PDataStorageRequestDataTest {
// correct GetDataRequestMessage with both sets of keys. // correct GetDataRequestMessage with both sets of keys.
@Test @Test
public void buildPreliminaryGetDataRequest_FilledP2PDataStore() throws NoSuchAlgorithmException { public void buildPreliminaryGetDataRequest_FilledP2PDataStore() throws NoSuchAlgorithmException {
PersistableNetworkPayload toAdd1 = new PersistableNetworkPayloadStub(new byte[] { 1 }); PersistableNetworkPayload toAdd1 = new PersistableNetworkPayloadStub(new byte[]{1});
PersistableNetworkPayload toAdd2 = new PersistableNetworkPayloadStub(new byte[] { 2 }); PersistableNetworkPayload toAdd2 = new PersistableNetworkPayloadStub(new byte[]{2});
ProtectedStorageEntry toAdd3 = getProtectedStorageEntryForAdd(); ProtectedStorageEntry toAdd3 = getProtectedStorageEntryForAdd();
ProtectedStorageEntry toAdd4 = getProtectedStorageEntryForAdd(); ProtectedStorageEntry toAdd4 = getProtectedStorageEntryForAdd();
@ -147,8 +144,8 @@ public class P2PDataStorageRequestDataTest {
// correct GetDataRequestMessage with both sets of keys. // correct GetDataRequestMessage with both sets of keys.
@Test @Test
public void requestData_FilledP2PDataStore_GetUpdatedDataRequest() throws NoSuchAlgorithmException { public void requestData_FilledP2PDataStore_GetUpdatedDataRequest() throws NoSuchAlgorithmException {
PersistableNetworkPayload toAdd1 = new PersistableNetworkPayloadStub(new byte[] { 1 }); PersistableNetworkPayload toAdd1 = new PersistableNetworkPayloadStub(new byte[]{1});
PersistableNetworkPayload toAdd2 = new PersistableNetworkPayloadStub(new byte[] { 2 }); PersistableNetworkPayload toAdd2 = new PersistableNetworkPayloadStub(new byte[]{2});
ProtectedStorageEntry toAdd3 = getProtectedStorageEntryForAdd(); ProtectedStorageEntry toAdd3 = getProtectedStorageEntryForAdd();
ProtectedStorageEntry toAdd4 = getProtectedStorageEntryForAdd(); ProtectedStorageEntry toAdd4 = getProtectedStorageEntryForAdd();