From 99f639a5891d73eba2e41c736b50b596ebd90bed Mon Sep 17 00:00:00 2001 From: Steven Barclay Date: Sat, 3 Feb 2024 02:36:31 +0800 Subject: [PATCH] Replace MockitoSession with the Mockito test extension Now that 'mockito-junit-jupiter' has been added to the build test dependencies, we may replace the manual setup & tear down of a Mockito session with the 'MockitoExtension' JUnit 5 test extension, as the old Mockito JUnit 4 test runner was no longer available upon upgrading to Jupiter. This slightly simplifies the tests which use '@Mock', '@Spy', etc. --- .../dao/burningman/BurningManServiceTest.java | 12 +++-------- ...osalServiceP2PDataStorageListenerTest.java | 21 +++++-------------- ...2PDataStorageBuildGetDataResponseTest.java | 18 +++++----------- 3 files changed, 13 insertions(+), 38 deletions(-) diff --git a/core/src/test/java/bisq/core/dao/burningman/BurningManServiceTest.java b/core/src/test/java/bisq/core/dao/burningman/BurningManServiceTest.java index 840ff9d37b5..97f3fb03d0c 100644 --- a/core/src/test/java/bisq/core/dao/burningman/BurningManServiceTest.java +++ b/core/src/test/java/bisq/core/dao/burningman/BurningManServiceTest.java @@ -52,13 +52,13 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.MockitoSession; +import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.stubbing.Answer; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -91,8 +91,8 @@ public void testGetDecayedAmount() { } @Nested + @ExtendWith(MockitoExtension.class) public class BurnShareTest { - private MockitoSession mockitoSession; @Mock private DaoStateService daoStateService; @Mock @@ -104,18 +104,12 @@ public class BurnShareTest { @BeforeEach public void setUp() { - mockitoSession = Mockito.mockitoSession().initMocks(this).startMocking(); when(cyclesInDaoStateService.getChainHeightOfPastCycle(800000, BurningManService.NUM_CYCLES_BURN_AMOUNT_DECAY)) .thenReturn(750000); when(cyclesInDaoStateService.getChainHeightOfPastCycle(800000, BurningManService.NUM_CYCLES_COMP_REQUEST_DECAY)) .thenReturn(700000); } - @AfterEach - public void tearDown() { - mockitoSession.finishMocking(); - } - private void addProofOfBurnTxs(Tx... txs) { var txsById = Arrays.stream(txs) .collect(Collectors.toMap(Tx::getId, tx -> tx)); diff --git a/core/src/test/java/bisq/core/dao/governance/proposal/ProposalServiceP2PDataStorageListenerTest.java b/core/src/test/java/bisq/core/dao/governance/proposal/ProposalServiceP2PDataStorageListenerTest.java index e459d6b1fa3..476819fedc9 100644 --- a/core/src/test/java/bisq/core/dao/governance/proposal/ProposalServiceP2PDataStorageListenerTest.java +++ b/core/src/test/java/bisq/core/dao/governance/proposal/ProposalServiceP2PDataStorageListenerTest.java @@ -36,23 +36,22 @@ import java.util.Collections; import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoSession; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.mockito.Mockito.*; - /** * Tests of the P2PDataStorage::onRemoved callback behavior to ensure that the proper number of signal events occur. */ +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) // the two stubs in setUp() are not used in every test public class ProposalServiceP2PDataStorageListenerTest { - private MockitoSession mockitoSession; - private ProposalService proposalService; @Mock private PeriodService periodService; @@ -63,11 +62,6 @@ public class ProposalServiceP2PDataStorageListenerTest { @BeforeEach public void setUp() { - mockitoSession = Mockito.mockitoSession() - .initMocks(this) - .strictness(Strictness.LENIENT) // the two stubs below are not used in every test - .startMocking(); - this.proposalService = new ProposalService( mock(P2PService.class), this.periodService, @@ -83,11 +77,6 @@ public void setUp() { when(this.daoStateService.isParseBlockChainComplete()).thenReturn(false); } - @AfterEach - public void tearDown() { - mockitoSession.finishMocking(); - } - private static ProtectedStorageEntry buildProtectedStorageEntry() { ProtectedStorageEntry protectedStorageEntry = mock(ProtectedStorageEntry.class); TempProposalPayload tempProposalPayload = mock(TempProposalPayload.class); diff --git a/p2p/src/test/java/bisq/network/p2p/storage/P2PDataStorageBuildGetDataResponseTest.java b/p2p/src/test/java/bisq/network/p2p/storage/P2PDataStorageBuildGetDataResponseTest.java index 1df313a4ea4..c50d363cfd2 100644 --- a/p2p/src/test/java/bisq/network/p2p/storage/P2PDataStorageBuildGetDataResponseTest.java +++ b/p2p/src/test/java/bisq/network/p2p/storage/P2PDataStorageBuildGetDataResponseTest.java @@ -45,13 +45,13 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoSession; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -62,8 +62,9 @@ import static org.mockito.Mockito.withSettings; public class P2PDataStorageBuildGetDataResponseTest { + @ExtendWith(MockitoExtension.class) + @MockitoSettings(strictness = Strictness.LENIENT) // there are unused stubs in TestState & elsewhere abstract static class P2PDataStorageBuildGetDataResponseTestBase { - private MockitoSession mockitoSession; // GIVEN null & non-null supportedCapabilities private TestState testState; @@ -76,10 +77,6 @@ abstract static class P2PDataStorageBuildGetDataResponseTestBase { @BeforeEach public void setUp() { - mockitoSession = Mockito.mockitoSession() - .initMocks(this) - .strictness(Strictness.LENIENT) // there are unused stubs in TestState & elsewhere - .startMocking(); this.testState = new TestState(); this.localNodeAddress = new NodeAddress("localhost", 8080); @@ -89,11 +86,6 @@ public void setUp() { Capabilities.app.addAll(Capability.MEDIATION); } - @AfterEach - public void tearDown() { - mockitoSession.finishMocking(); - } - static class RequiredCapabilitiesPNPStub extends PersistableNetworkPayloadStub implements CapabilityRequiringPayload { Capabilities capabilities;