Skip to content

Commit

Permalink
Change the strictness of some Mockito instances to LENIENT
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Sep 18, 2023
1 parent 2409b79 commit c2921ae
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public final class JobEventRdbSearchTest {

@Mock
private PreparedStatement preparedStatement;

@Mock

// TODO We should not use `Mock.Strictness.LENIENT` here, but the default. This is a flaw in the unit test design.
@Mock(strictness = Mock.Strictness.LENIENT)
private ResultSet resultSet;

@Mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -70,7 +71,7 @@ public final class SchedulerEngineTest {
public void setUp() {
schedulerEngine = new SchedulerEngine(taskScheduler, facadeService, new JobTracingEventBus(), frameworkIDService, statisticManager);
ReflectionUtils.setFieldValue(schedulerEngine, "facadeService", facadeService);
when(facadeService.load("test_job")).thenReturn(Optional.of(CloudJobConfigurationBuilder.createCloudJobConfiguration("test_job")));
lenient().when(facadeService.load("test_job")).thenReturn(Optional.of(CloudJobConfigurationBuilder.createCloudJobConfiguration("test_job")));
new RunningService(mock(CoordinatorRegistryCenter.class)).clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -76,7 +77,7 @@ public final class TaskLaunchScheduledServiceTest {

@BeforeEach
public void setUp() {
when(facadeService.loadAppConfig("test_app")).thenReturn(Optional.of(CloudAppConfigurationBuilder.createCloudAppConfiguration("test_app")));
lenient().when(facadeService.loadAppConfig("test_app")).thenReturn(Optional.of(CloudAppConfigurationBuilder.createCloudAppConfiguration("test_app")));
taskLaunchScheduledService = new TaskLaunchScheduledService(schedulerDriver, taskScheduler, facadeService, jobTracingEventBus);
taskLaunchScheduledService.startUp();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -50,7 +50,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public final class ProducerManagerTest {

@Mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -242,7 +243,7 @@ private ShardingContexts createMultipleShardingContexts() {
private void prepareForIsNotMisfire(final JobFacade jobFacade, final ShardingContexts shardingContexts) {
when(jobFacade.getShardingContexts()).thenReturn(shardingContexts);
when(jobFacade.misfireIfRunning(shardingContexts.getShardingItemParameters().keySet())).thenReturn(false);
when(jobFacade.isExecuteMisfired(shardingContexts.getShardingItemParameters().keySet())).thenReturn(false);
lenient().when(jobFacade.isExecuteMisfired(shardingContexts.getShardingItemParameters().keySet())).thenReturn(false);
}

private void verifyForIsNotMisfire(final JobFacade jobFacade, final ShardingContexts shardingContexts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -60,8 +61,9 @@ public final class HttpJobExecutorTest {

@Mock
private JobFacade jobFacade;

@Mock

// TODO We should not use `Mock.Strictness.LENIENT` here, but the default. This is a flaw in the unit test design.
@Mock(strictness = Mock.Strictness.LENIENT)
private Properties properties;

@Mock
Expand All @@ -80,7 +82,7 @@ public static void init() {

@BeforeEach
public void setUp() {
when(jobConfig.getProps()).thenReturn(properties);
lenient().when(jobConfig.getProps()).thenReturn(properties);
jobExecutor = new HttpJobExecutor();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public final class JobOperateAPIImplTest {
static final int DUMP_PORT = 9000;

private JobOperateAPI jobOperateAPI;

@Mock

// TODO We should not use `Mock.Strictness.LENIENT` here, but the default. This is a flaw in the unit test design.
@Mock(strictness = Mock.Strictness.LENIENT)
private CoordinatorRegistryCenter regCenter;

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
public final class JobStatisticsAPIImplTest {

private JobStatisticsAPI jobStatisticsAPI;

@Mock

// TODO We should not use `Mock.Strictness.LENIENT` here, but the default. This is a flaw in the unit test design.
@Mock(strictness = Mock.Strictness.LENIENT)
private CoordinatorRegistryCenter regCenter;

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
public final class ShardingStatisticsAPIImplTest {

private ShardingStatisticsAPI shardingStatisticsAPI;

@Mock

// TODO We should not use `Mock.Strictness.LENIENT` here, but the default. This is a flaw in the unit test design.
@Mock(strictness = Mock.Strictness.LENIENT)
private CoordinatorRegistryCenter regCenter;

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public void reset() {
public void tearDown() {
JobRegistry.getInstance().shutdown(simpleJobName);
}

@Test
public void assertSpringJobBean() {
assertSimpleElasticJobBean();
}

private void assertSimpleElasticJobBean() {
Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() ->
Awaitility.await().atMost(5L, TimeUnit.SECONDS).untilAsserted(() ->
assertThat(AnnotationSimpleJob.isCompleted(), is(true))
);
assertTrue(AnnotationSimpleJob.isCompleted());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit.jupiter.SpringExtension;

/**
* Background reference AbstractJUnit4SpringContextTests
* and <a href="https://github.com/spring-projects/spring-framework/issues/29149">spring-projects/spring-framework#29149</a>.
*
* @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
*/
@ExtendWith(SpringExtension.class)
@TestExecutionListeners(EmbedZookeeperTestExecutionListener.class)
@TestExecutionListeners(listeners = {EmbedZookeeperTestExecutionListener.class},
inheritListeners = false,
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public abstract class AbstractZookeeperJUnitJupiterSpringContextTests {
}

0 comments on commit c2921ae

Please sign in to comment.