Class Neo4jRule

java.lang.Object
org.neo4j.harness.junit.rule.Neo4jRule
All Implemented Interfaces:
org.junit.rules.TestRule
Direct Known Subclasses:
EnterpriseNeo4jRule

public class Neo4jRule extends Object implements org.junit.rules.TestRule
Community Neo4j JUnit rule. Allows easily start neo4j instance for testing purposes with various user-provided options and configurations.

By default it will try to start neo4j with embedded web server on random ports. Therefore it is necessary for the test code to use httpURI() and then URI.resolve(String) to create the URIs to be invoked.

In case if starting embedded web server is not desirable it can be fully disabled by using withDisabledServer() configuration option.

  • Constructor Details

    • Neo4jRule

      public Neo4jRule()
    • Neo4jRule

      public Neo4jRule(Path workingDirectory)
  • Method Details

    • apply

      public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
    • withConfig

      public <T> Neo4jRule withConfig(Setting<T> key, T value)
      Configure the Neo4j instance. Configuration here can be both configuration aimed at the server as well as the database tuning options. Please refer to the Neo4j Manual for details on available configuration options.
      Type Parameters:
      T - the type of the setting
      Parameters:
      key - the config key
      value - the config value
      Returns:
      this configurator instance
    • withUnmanagedExtension

      public Neo4jRule withUnmanagedExtension(String mountPath, Class<?> extension)
      Shortcut for configuring the server to use an unmanaged extension. Please refer to the Neo4j Manual on how to write unmanaged extensions.
      Parameters:
      mountPath - the http path, relative to the server base URI, that this extension should be mounted at.
      extension - the unmanaged extension class.
      Returns:
      this configurator instance
    • withUnmanagedExtension

      public Neo4jRule withUnmanagedExtension(String mountPath, String packageName)
      Shortcut for configuring the server to find and mount all unmanaged extensions in the given package.
      Parameters:
      mountPath - the http path, relative to the server base URI, that this extension should be mounted at.
      packageName - a java package with extension classes.
      Returns:
      this configurator instance
      See Also:
    • withDisabledServer

      public Neo4jRule withDisabledServer()
      Disable web server on configured Neo4j instance. For cases where web server is not required to test specific functionality it can be fully disabled using this tuning option.
      Returns:
      this configurator instance.
    • withFixture

      public Neo4jRule withFixture(Path cypherFileOrDirectory)
      Data fixtures to inject upon server build. This can be either a file with a plain-text cypher query (for example, myFixture.cyp), or a directory containing such files with the suffix ".cyp".
      Parameters:
      cypherFileOrDirectory - file with cypher statement, or directory containing ".cyp"-suffixed files.
      Returns:
      this configurator instance
    • withFixture

      public Neo4jRule withFixture(String fixtureStatement)
      Data fixture to inject upon server build. This should be a valid Cypher statement.
      Parameters:
      fixtureStatement - a cypher statement
      Returns:
      this configurator instance
    • withFixture

      public Neo4jRule withFixture(Function<GraphDatabaseService,Void> fixtureFunction)
      Data fixture to inject upon server build. This should be a user implemented fixture function operating on a GraphDatabaseService instance
      Parameters:
      fixtureFunction - a fixture function
      Returns:
      this configurator instance
    • copyFrom

      public Neo4jRule copyFrom(Path sourceDirectory)
      Pre-populate the server with databases copied from the specified source directory. The source directory needs to have sub-folders `databases/neo4j` in which the source store files are located.
      Parameters:
      sourceDirectory - the directory to copy from
      Returns:
      this configurator instance
    • withProcedure

      public Neo4jRule withProcedure(Class<?> procedureClass)
      Configure the server to load the specified procedure definition class. The class should contain one or more methods annotated with Procedure, these will become available to call through cypher.
      Parameters:
      procedureClass - a class containing one or more procedure definitions
      Returns:
      this configurator instance
    • withFunction

      public Neo4jRule withFunction(Class<?> functionClass)
      Configure the server to load the specified function definition class. The class should contain one or more methods annotated with UserFunction, these will become available to call through cypher.
      Parameters:
      functionClass - a class containing one or more function definitions
      Returns:
      this configurator instance
    • withAggregationFunction

      public Neo4jRule withAggregationFunction(Class<?> functionClass)
      Configure the server to load the specified aggregation function definition class. The class should contain one or more methods annotated with UserAggregationFunction, these will become available to call through cypher.
      Parameters:
      functionClass - a class containing one or more function definitions
      Returns:
      this configurator instance
    • dumpLogsOnFailure

      public Neo4jRule dumpLogsOnFailure(PrintStream out)
      Dump available logs on failure.
      Parameters:
      out - stream used to dump logs into.
      Returns:
      this configurator instance
    • dumpLogsOnFailure

      public Neo4jRule dumpLogsOnFailure(Supplier<PrintStream> out)
      Dump available logs on failure.

      Similar to dumpLogsOnFailure(PrintStream), but permits late-binding the stream, or producing the stream based on some computation.

      Parameters:
      out - the supplier of the stream that will be used to dump logs info.
      Returns:
      this configurator instance.
    • boltURI

      public URI boltURI()
      Returns the URI to the Bolt Protocol connector of the instance.
      Returns:
      the bolt address.
    • httpURI

      public URI httpURI()
      Returns the URI to the root resource of the instance. For example, http://localhost:7474/
      Returns:
      the http address to the root resource.
    • httpsURI

      public URI httpsURI()
      Returns ths URI to the root resource of the instance using the https protocol. For example, https://localhost:7475/.
      Returns:
      the https address to the root resource.
    • databaseManagementService

      public DatabaseManagementService databaseManagementService()
      Access the DatabaseManagementService used by the server.
      Returns:
      the database management service backing this instance.
    • defaultDatabaseService

      public GraphDatabaseService defaultDatabaseService()
      Access default database service.
      Returns:
      default database service.
    • config

      public Configuration config()
      Returns the server's configuration.
      Returns:
      the current configuration of the instance.