Skip to content

Commit

Permalink
Do not use inlined mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Koval committed May 2, 2018
1 parent 4ceba71 commit 55fd038
Show file tree
Hide file tree
Showing 31 changed files with 98 additions and 52 deletions.
Expand Up @@ -94,7 +94,8 @@ private static Channel channelMock( boolean open )
{
Channel channel = mock( Channel.class );
when( channel.isOpen() ).thenReturn( open );
when( channel.close() ).thenReturn( mock( ChannelFuture.class ) );
ChannelFuture channelFuture = mock( ChannelFuture.class );
when( channel.close() ).thenReturn( channelFuture );
return channel;
}
}
Expand Up @@ -102,7 +102,8 @@ private static void init( BoltStateMachine machine ) throws AuthenticationExcept

private static void init( BoltStateMachine machine, String owner ) throws AuthenticationException, BoltConnectionFatality
{
when( machine.spi.authenticate( any() ) ).thenReturn( mock( AuthenticationResult.class ) );
AuthenticationResult authenticationResult = mock( AuthenticationResult.class );
when( machine.spi.authenticate( any() ) ).thenReturn( authenticationResult );
machine.init( USER_AGENT, owner == null ? emptyMap() : Collections.singletonMap( AuthToken.PRINCIPAL, owner ), nullResponseHandler() );
}

Expand Down
Expand Up @@ -574,7 +574,8 @@ private static TransactionStateMachineSPI newFailingTransactionStateMachineSPI(
TransactionStateMachine.BoltResultHandle resultHandle = newResultHandle();
TransactionStateMachineSPI stateMachineSPI = mock( TransactionStateMachineSPI.class );

when( stateMachineSPI.beginTransaction( any() ) ).thenReturn( mock( KernelTransaction.class ) );
KernelTransaction kernelTransaction = mock( KernelTransaction.class );
when( stateMachineSPI.beginTransaction( any() ) ).thenReturn( kernelTransaction );
when( stateMachineSPI.executeQuery( any(), any(), anyString(), any() ) ).thenReturn( resultHandle );
when( stateMachineSPI.executeQuery( any(), any(), eq( "FAIL" ), any() ) ).thenThrow( new TransactionTerminatedException( failureStatus ) );

Expand Down
Expand Up @@ -74,7 +74,8 @@ public void setUp() throws Exception
when( kernelStatement.getVersionContext() ).thenReturn( versionContext );
when( transactionalContext.statement() ).thenReturn( kernelStatement );
Result result = mock( Result.class );
when( result.getQueryStatistics() ).thenReturn( mock( QueryStatistics.class ) );
QueryStatistics statistics = mock( QueryStatistics.class );
when( result.getQueryStatistics() ).thenReturn( statistics );
when( executor.execute( any(), anyMap(), any() ) ).thenReturn( result );
}

Expand Down
Expand Up @@ -62,9 +62,10 @@ public void defaultsToCsvWhenModeNotSpecified() throws Exception
{
File homeDir = testDir.directory( "home" );
ImporterFactory mockImporterFactory = mock( ImporterFactory.class );
Importer importer = mock( Importer.class );
when( mockImporterFactory
.getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) ) )
.thenReturn( mock( Importer.class ) );
.thenReturn( importer );

try ( RealOutsideWorld outsideWorld = new RealOutsideWorld( System.out, System.err, new ByteArrayInputStream( new byte[0] ) ) )
{
Expand All @@ -86,9 +87,10 @@ public void acceptsNodeMetadata() throws Exception
{
File homeDir = testDir.directory( "home" );
ImporterFactory mockImporterFactory = mock( ImporterFactory.class );
Importer importer = mock( Importer.class );
when( mockImporterFactory
.getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) ) )
.thenReturn( mock( Importer.class ) );
.thenReturn( importer );

ImportCommand importCommand =
new ImportCommand( homeDir.toPath(), testDir.directory( "conf" ).toPath(),
Expand All @@ -107,9 +109,10 @@ public void acceptsRelationshipsMetadata() throws Exception
{
File homeDir = testDir.directory( "home" );
ImporterFactory mockImporterFactory = mock( ImporterFactory.class );
Importer importer = mock( Importer.class );
when( mockImporterFactory
.getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) ) )
.thenReturn( mock( Importer.class ) );
.thenReturn( importer );

ImportCommand importCommand =
new ImportCommand( homeDir.toPath(), testDir.directory( "conf" ).toPath(),
Expand Down
Expand Up @@ -54,7 +54,8 @@ public class TestPlaceboTransaction
public void before()
{
ThreadToStatementContextBridge bridge = mock( ThreadToStatementContextBridge.class );
when( bridge.get() ).thenReturn( mock( Statement.class ) );
Statement statement = mock( Statement.class );
when( bridge.get() ).thenReturn( statement );
kernelTransaction = spy( KernelTransaction.class );
locks = mock( Locks.class );
when(kernelTransaction.locks()).thenReturn( locks );
Expand Down
Expand Up @@ -76,7 +76,8 @@ public void shouldOnlyCreateOneApplierPerProvider() throws Exception
when( commitment.hasExplicitIndexChanges() ).thenReturn( true );
IndexConfigStore config = newIndexConfigStore( names, applierName );
ExplicitIndexApplierLookup applierLookup = mock( ExplicitIndexApplierLookup.class );
when( applierLookup.newApplier( anyString(), anyBoolean() ) ).thenReturn( mock( TransactionApplier.class ) );
TransactionApplier transactionApplier = mock( TransactionApplier.class );
when( applierLookup.newApplier( anyString(), anyBoolean() ) ).thenReturn( transactionApplier );
try ( ExplicitBatchIndexApplier applier = new ExplicitBatchIndexApplier( config, applierLookup, BYPASS, INTERNAL ) )
{
TransactionToApply tx = new TransactionToApply( null, 2 );
Expand Down Expand Up @@ -104,7 +105,8 @@ public void shouldOrderTransactionsMakingExplicitIndexChanges() throws Throwable
Map<String,Integer> keys = MapUtil.genericMap( "key", 0 );
String applierName = "test-applier";
ExplicitIndexApplierLookup applierLookup = mock( ExplicitIndexApplierLookup.class );
when( applierLookup.newApplier( anyString(), anyBoolean() ) ).thenReturn( mock( TransactionApplier.class ) );
TransactionApplier transactionApplier = mock( TransactionApplier.class );
when( applierLookup.newApplier( anyString(), anyBoolean() ) ).thenReturn( transactionApplier );
IndexConfigStore config = newIndexConfigStore( names, applierName );

// WHEN multiple explicit index transactions are running, they should be done in order
Expand Down
Expand Up @@ -111,7 +111,8 @@ public void before() throws Exception
collectionsFactory = Mockito.spy( new TestCollectionsFactory() );
when( headerInformation.getAdditionalHeader() ).thenReturn( new byte[0] );
when( headerInformationFactory.create() ).thenReturn( headerInformation );
when( readLayer.newStatement() ).thenReturn( mock( StoreStatement.class ) );
StoreStatement statement = mock( StoreStatement.class );
when( readLayer.newStatement() ).thenReturn( statement );
when( neoStores.getMetaDataStore() ).thenReturn( metaDataStore );
when( storageEngine.storeReadLayer() ).thenReturn( readLayer );
doAnswer( invocation -> ((Collection<StorageCommand>) invocation.getArgument(0) ).add( new Command
Expand Down
Expand Up @@ -36,14 +36,14 @@

import org.neo4j.graphdb.DatabaseShutdownException;
import org.neo4j.graphdb.security.AuthorizationExpiredException;
import org.neo4j.internal.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.internal.kernel.api.security.LoginContext;
import org.neo4j.io.pagecache.tracing.cursor.context.EmptyVersionContextSupplier;
import org.neo4j.io.pagecache.tracing.cursor.context.VersionContextSupplier;
import org.neo4j.kernel.AvailabilityGuard;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.KernelTransactionHandle;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.internal.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.api.explicitindex.AutoIndexing;
import org.neo4j.kernel.api.security.AnonymousContext;
import org.neo4j.kernel.impl.api.index.IndexingService;
Expand Down Expand Up @@ -533,7 +533,8 @@ private static KernelTransactions newKernelTransactions( boolean testKernelTrans
StorageStatement... otherStorageStatements ) throws Throwable
{
Locks locks = mock( Locks.class );
when( locks.newClient() ).thenReturn( mock( Locks.Client.class ) );
Locks.Client client = mock( Locks.Client.class );
when( locks.newClient() ).thenReturn( client );

StoreReadLayer readLayer = mock( StoreReadLayer.class );
when( readLayer.newStatement() ).thenReturn( firstStoreStatements, otherStorageStatements );
Expand Down
Expand Up @@ -364,7 +364,8 @@ private KernelTransactionImplementation createTransaction()
when( transaction.statementLocks() ).thenReturn( locks );
when( transaction.tokenRead() ).thenReturn( tokenRead );
when( transaction.schemaRead() ).thenReturn( schemaRead );
when( transaction.txState() ).thenReturn( mock( TransactionState.class ) );
TransactionState transactionState = mock( TransactionState.class );
when( transaction.txState() ).thenReturn( transactionState );
return transaction;
}
}
Expand Up @@ -207,12 +207,14 @@ public void shouldBeAbleToRecoverAndUpdateOnlineIndex() throws Exception
public void shouldKeepFailedIndexesAsFailedAfterRestart() throws Exception
{
// Given
IndexPopulator indexPopulator = mock( IndexPopulator.class );
when( mockedIndexProvider
.getPopulator( anyLong(), any( SchemaIndexDescriptor.class ), any( IndexSamplingConfig.class ) ) )
.thenReturn( mock( IndexPopulator.class ) );
.thenReturn( indexPopulator );
IndexAccessor indexAccessor = mock( IndexAccessor.class );
when( mockedIndexProvider.getOnlineAccessor(
anyLong(), any( SchemaIndexDescriptor.class ), any( IndexSamplingConfig.class ) )
).thenReturn( mock( IndexAccessor.class ) );
).thenReturn( indexAccessor );
startDb();
createIndex( myLabel );
rotateLogsAndCheckPoint();
Expand Down
Expand Up @@ -72,6 +72,7 @@
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingController;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode;
import org.neo4j.kernel.impl.scheduler.CentralJobScheduler;
import org.neo4j.kernel.impl.store.UnderlyingStorageException;
import org.neo4j.kernel.impl.store.record.IndexRule;
import org.neo4j.kernel.impl.storemigration.StoreMigrationParticipant;
Expand All @@ -80,7 +81,6 @@
import org.neo4j.kernel.impl.transaction.state.DefaultIndexProviderMap;
import org.neo4j.kernel.impl.transaction.state.DirectIndexUpdates;
import org.neo4j.kernel.impl.transaction.state.IndexUpdates;
import org.neo4j.kernel.impl.scheduler.CentralJobScheduler;
import org.neo4j.kernel.lifecycle.LifeRule;
import org.neo4j.kernel.lifecycle.LifecycleException;
import org.neo4j.logging.AssertableLogProvider;
Expand Down Expand Up @@ -362,8 +362,9 @@ public void shouldLogIndexStateOnInit() throws Exception
// given
IndexProvider provider = mock( IndexProvider.class );
when( provider.getProviderDescriptor() ).thenReturn( PROVIDER_DESCRIPTOR );
IndexAccessor indexAccessor = mock( IndexAccessor.class );
when( provider.getOnlineAccessor( anyLong(), any( SchemaIndexDescriptor.class ), any( IndexSamplingConfig.class ) ) )
.thenReturn( mock( IndexAccessor.class ) );
.thenReturn( indexAccessor );
IndexProviderMap providerMap = new DefaultIndexProviderMap( provider );
TokenNameLookup mockLookup = mock( TokenNameLookup.class );

Expand Down Expand Up @@ -924,8 +925,9 @@ public void shouldLogIndexStateOutliersOnInit() throws Exception
// given
IndexProvider provider = mock( IndexProvider.class );
when( provider.getProviderDescriptor() ).thenReturn( PROVIDER_DESCRIPTOR );
IndexAccessor indexAccessor = mock( IndexAccessor.class );
when( provider.getOnlineAccessor( anyLong(), any( SchemaIndexDescriptor.class ), any( IndexSamplingConfig.class ) ) )
.thenReturn( mock( IndexAccessor.class ) );
.thenReturn( indexAccessor );
IndexProviderMap providerMap = new DefaultIndexProviderMap( provider );
TokenNameLookup mockLookup = mock( TokenNameLookup.class );

Expand Down Expand Up @@ -972,8 +974,9 @@ public void shouldLogIndexStateOutliersOnStart() throws Exception
// given
IndexProvider provider = mock( IndexProvider.class );
when( provider.getProviderDescriptor() ).thenReturn( PROVIDER_DESCRIPTOR );
IndexAccessor indexAccessor = mock( IndexAccessor.class );
when( provider.getOnlineAccessor( anyLong(), any( SchemaIndexDescriptor.class ), any( IndexSamplingConfig.class ) ) )
.thenReturn( mock( IndexAccessor.class ) );
.thenReturn( indexAccessor );
IndexProviderMap providerMap = new DefaultIndexProviderMap( provider );
TokenNameLookup mockLookup = mock( TokenNameLookup.class );

Expand Down
Expand Up @@ -115,7 +115,8 @@ public void setup() throws IndexNotFoundKernelException
when( readOps.txState() ).thenReturn( txState );

IndexingService indexingService = mock( IndexingService.class );
when( indexingService.getIndexProxy( any( SchemaDescriptor.class ) ) ).thenReturn( mock( IndexProxy.class ) );
IndexProxy indexProxy = mock( IndexProxy.class );
when( indexingService.getIndexProxy( any( SchemaDescriptor.class ) ) ).thenReturn( indexProxy );
indexTxUpdater = new IndexTxStateUpdater( storeReadLayer, readOps, indexingService );

}
Expand Down
Expand Up @@ -121,8 +121,9 @@ public void setUp() throws InvalidTransactionTypeKernelException
when( cursors.allocatePropertyCursor() ).thenReturn( propertyCursor );
when( cursors.allocateRelationshipScanCursor() ).thenReturn( relationshipCursor );
AutoIndexing autoindexing = mock( AutoIndexing.class );
when( autoindexing.nodes() ).thenReturn( mock( AutoIndexOperations.class ) );
when( autoindexing.relationships() ).thenReturn( mock( AutoIndexOperations.class ) );
AutoIndexOperations autoIndexOperations = mock( AutoIndexOperations.class );
when( autoindexing.nodes() ).thenReturn( autoIndexOperations );
when( autoindexing.relationships() ).thenReturn( autoIndexOperations );
StorageStatement storageStatement = mock( StorageStatement.class );
StorageEngine engine = mock( StorageEngine.class );
storeReadLayer = mock( StoreReadLayer.class );
Expand Down
Expand Up @@ -81,7 +81,8 @@ public void shouldUpdateLabelStoreScanOnNodeCommands() throws Exception
after.setLabelField( 18, emptyDynamicRecords );
final Command.NodeCommand command = new Command.NodeCommand( before, after );

when( labelScanStore.get() ).thenReturn( mock( LabelScanWriter.class ) );
LabelScanWriter labelScanWriter = mock( LabelScanWriter.class );
when( labelScanStore.get() ).thenReturn( labelScanWriter );

// when
boolean result;
Expand Down
Expand Up @@ -56,11 +56,16 @@ public void shouldClearStateOnClose()
{
// GIVEN
NeoStores mockStore = mock( NeoStores.class );
when( mockStore.getNodeStore() ).thenReturn( mock( NodeStore.class ) );
when( mockStore.getRelationshipStore() ).thenReturn( mock( RelationshipStore.class ) );
when( mockStore.getPropertyStore() ).thenReturn( mock( PropertyStore.class ) );
when( mockStore.getSchemaStore() ).thenReturn( mock( SchemaStore.class ) );
when( mockStore.getRelationshipGroupStore() ).thenReturn( mock( RelationshipGroupStore.class ) );
NodeStore store = mock( NodeStore.class );
when( mockStore.getNodeStore() ).thenReturn( store );
RelationshipStore relationshipStore = mock( RelationshipStore.class );
when( mockStore.getRelationshipStore() ).thenReturn( relationshipStore );
PropertyStore propertyStore = mock( PropertyStore.class );
when( mockStore.getPropertyStore() ).thenReturn( propertyStore );
SchemaStore schemaStore = mock( SchemaStore.class );
when( mockStore.getSchemaStore() ).thenReturn( schemaStore );
RelationshipGroupStore groupStore = mock( RelationshipGroupStore.class );
when( mockStore.getRelationshipGroupStore() ).thenReturn( groupStore );

RecordChangeSet changeSet = new RecordChangeSet( new Loaders( mockStore ) );

Expand Down
Expand Up @@ -190,9 +190,12 @@ private TransactionRecordState injectAllPossibleCommands()
Collections.emptyList() );

NeoStores neoStores = mock( NeoStores.class );
when( neoStores.getNodeStore() ).thenReturn( mock( NodeStore.class ) );
when( neoStores.getRelationshipGroupStore() ).thenReturn( mock( RelationshipGroupStore.class ) );
when( neoStores.getRelationshipStore() ).thenReturn( mock( RelationshipStore.class ) );
NodeStore store = mock( NodeStore.class );
when( neoStores.getNodeStore() ).thenReturn( store );
RelationshipGroupStore relationshipGroupStore = mock( RelationshipGroupStore.class );
when( neoStores.getRelationshipGroupStore() ).thenReturn( relationshipGroupStore );
RelationshipStore relationshipStore = mock( RelationshipStore.class );
when( neoStores.getRelationshipStore() ).thenReturn( relationshipStore );

return new TransactionRecordState( neoStores, mock( IntegrityValidator.class ), recordChangeSet,
0, null, null, null, null, null );
Expand Down
Expand Up @@ -51,7 +51,8 @@ public void shouldReportThirdPartyPackagesAtSpecifiedMount() throws Exception
CommunityNeoServer neoServer = mock( CommunityNeoServer.class );
when( neoServer.baseUri() ).thenReturn( new URI( "http://localhost:7575" ) );
when( neoServer.getWebServer() ).thenReturn( webServer );
when( neoServer.getDatabase() ).thenReturn( mock(Database.class));
Database database = mock( Database.class );
when( neoServer.getDatabase() ).thenReturn( database );

Config config = mock( Config.class );
List<ThirdPartyJaxRsPackage> jaxRsPackages = new ArrayList<>();
Expand Down
Expand Up @@ -50,8 +50,8 @@ public void shouldThrowSpecificExceptionOnConcurrentTransactionAccess() throws E
new TransactionHandleRegistry( mock( Clock.class), 0, NullLogProvider.getInstance() );
TransitionalPeriodTransactionMessContainer kernel = mock( TransitionalPeriodTransactionMessContainer.class );
GraphDatabaseQueryService queryService = mock( GraphDatabaseQueryService.class );
when(kernel.newTransaction( any( KernelTransaction.Type.class ), any( LoginContext.class ), anyLong() ) )
.thenReturn( mock(TransitionalTxManagementKernelTransaction.class) );
TransitionalTxManagementKernelTransaction kernelTransaction = mock( TransitionalTxManagementKernelTransaction.class );
when(kernel.newTransaction( any( KernelTransaction.Type.class ), any( LoginContext.class ), anyLong() ) ).thenReturn( kernelTransaction );
TransactionFacade actions = new TransactionFacade( kernel, null, queryService, registry, NullLogProvider.getInstance() );

final TransactionHandle transactionHandle =
Expand Down
Expand Up @@ -71,7 +71,8 @@ public void shouldRespondWithCompleteStreamOfTransactions() throws Exception
// given
when( transactionIdStore.getLastCommittedTransactionId() ).thenReturn( 15L );
when( logicalTransactionStore.getTransactions( 14L ) ).thenReturn( txCursor( cursor( tx( 14 ), tx( 15 ) ) ) );
when( context.writeAndFlush( any() ) ).thenReturn( mock( ChannelFuture.class ) );
ChannelFuture channelFuture = mock( ChannelFuture.class );
when( context.writeAndFlush( any() ) ).thenReturn( channelFuture );

// when
txPullRequestHandler.channelRead0( context, new TxPullRequest( 13, storeId ) );
Expand Down

0 comments on commit 55fd038

Please sign in to comment.