Skip to content

Commit

Permalink
replace Mock annotation usage with method scope local Mockito.mock() …
Browse files Browse the repository at this point in the history
…invocation
  • Loading branch information
NathanQingyangXu committed Oct 21, 2024
1 parent 2cb640e commit 540b89e
Showing 1 changed file with 5 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,24 @@
import com.mongodb.internal.dns.DnsResolver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mock;

import static java.util.Collections.singletonList;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;

/**
* See https://github.com/mongodb/specifications/blob/master/source/initial-dns-seedlist-discovery/tests/README.md
*/
class InitialDnsSeedListDiscoveryProseTest {
private static final String SRV_SERVICE_NAME = "mongodb";

@Mock
private ClusterableServerFactory serverFactory;

@Mock
private DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory;

private DnsMultiServerCluster cluster;

@BeforeEach
void setUp() throws Exception {
try (AutoCloseable ignored = openMocks(this)) {
when(serverFactory.getSettings()).thenReturn(ServerSettings.builder().build());
when(serverFactory.create(any(Cluster.class), any(ServerAddress.class))).thenReturn(mock(ClusterableServer.class));
}
}

@AfterEach
void tearDown() {
if (cluster != null) {
Expand Down Expand Up @@ -118,6 +101,7 @@ private void doTest(final String srvHost, final String resolvedHost, final boole
final DnsResolver dnsResolver = new DefaultDnsResolver((name, type) -> singletonList(String.format("10 5 27017 %s",
resolvedHost)));

final DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
when(dnsSrvRecordMonitorFactory.create(eq(srvHost), eq(SRV_SERVICE_NAME), any(DnsSrvRecordInitializer.class))).thenAnswer(
invocation -> new DefaultDnsSrvRecordMonitor(srvHost, SRV_SERVICE_NAME, 10, 10,
invocation.getArgument(2), clusterId, dnsResolver));
Expand All @@ -127,6 +111,10 @@ private void doTest(final String srvHost, final String resolvedHost, final boole
.requiredClusterType(ClusterType.SHARDED)
.srvHost(srvHost);

final ClusterableServerFactory serverFactory = mock(ClusterableServerFactory.class);
when(serverFactory.getSettings()).thenReturn(ServerSettings.builder().build());
when(serverFactory.create(any(Cluster.class), any(ServerAddress.class))).thenReturn(mock(ClusterableServer.class));

cluster = new DnsMultiServerCluster(clusterId, settingsBuilder.build(),
serverFactory,
dnsSrvRecordMonitorFactory);
Expand Down

0 comments on commit 540b89e

Please sign in to comment.