Class Neo4jExtension

java.lang.Object
org.neo4j.harness.junit.extension.Neo4jExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver
Direct Known Subclasses:
EnterpriseNeo4jExtension

public class Neo4jExtension extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver
Community Neo4j JUnit 5 Extension. Allows easily start neo4j instance for testing purposes with junit 5 with various user-provided options and configurations. Can be registered declaratively with ExtendWith or programmatically using RegisterExtension.

By default it will try to start neo4j with embedded web server on random ports. In case if more advance configuration is required please use programmatical extension registration and configure desired Neo4j behaviour using available options.

Please note that neo4j server uses dynamic ports and it is necessary for the test code to use Neo4j.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 Neo4jExtensionBuilder.withDisabledServer().

Usage example:

  
  @ExtendWith( Neo4jExtension.class )
       class TestExample {
              @Test
               void testExample( Neo4j neo4j, GraphDatabaseService databaseService ) {
                   // test code
               }
       }
  
 
The extension follows the lifecycle of junit 5. If you define the extension with ExtendWith on the test class or RegisterExtension on a static field, the neo4j instance will start before any tests are executed, and stop after the last test finishes. If you define the extension with RegisterExtension on a non-static field, there will be a new instance per test method.

Example with per method instances:

  
  @TestInstance( TestInstance.Lifecycle.PER_METHOD ) // This is default, just there for clarity
       class TestExample {
             @RegisterExtension
              Neo4jExtension extension = Neo4jExtension.builder().build();

             @Test
              void testExample( Neo4j neo4j, GraphDatabaseService databaseService ) {
                  // test code
              }
       }
  
 
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    void
    beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
     
     
    resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    boolean
    supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
  • Constructor Details

    • Neo4jExtension

      public Neo4jExtension()
  • Method Details

    • builder

      public static Neo4jExtensionBuilder builder()
    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
    • afterAll

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Throws:
      Exception
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception
      Throws:
      Exception
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException