Skip to content

Commit

Permalink
Replace MockitoSession with the Mockito test extension
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stejbac committed Feb 2, 2024
1 parent c41903f commit 99f639a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -91,8 +91,8 @@ public void testGetDecayedAmount() {
}

@Nested
@ExtendWith(MockitoExtension.class)
public class BurnShareTest {
private MockitoSession mockitoSession;
@Mock
private DaoStateService daoStateService;
@Mock
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 99f639a

Please sign in to comment.