Component creates/removes seed data for tests. Mainly used within a spring context and spring data graph



This component came about after requiring a simply way of seeding test data for integration tests with Neo4j and Spring.


The best way to see how this can be used is with an example. Lets look at a simple spring backed application where seed data is required for tests.

Context file

Lets start with a simple context file which defines a test graph database, the transaction manager and the location of the spring data repositories (please note that even though the example is focusing on SDN backed applications it should be pretty straighforward to use the native api.)

<?xml version="1.0" encoding="UTF-8"?>
		<beans xmlns=""

		<neo4j:config graphDatabaseService="graphDatabaseService" />

		<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" />

		<tx:annotation-driven mode="aspectj" transaction-manager="neo4jTransactionManager" />

		<neo4j:repositories base-package="" />



Now that we've defined a context file lets take a look at the entities and the repositories:

public class UserEntity

    public UserEntity()

    private Long nodeId;

    @RelatedTo(elementClass = HobbyEntity.class, type = "HOBBY")
    private Set<HobbyEntity> hobbies;

    @RelatedTo(elementClass = AddressEntity.class, type = "ADDRESS")
    private AddressEntity address;

    @Indexed(indexName = "usernameIndex")
    private String username;

    @Indexed(indexName = "publicIdIndex")
    private String publicId;

    ... rest omitted

This simple example shows the central entity as the User. The user has a set of hobbies, an address, a nodeId (which is dynamicaly created by Neo4j), a username with an index called username and a publicId also with an index. Please note that the indexName must be provided if you're using SDN. It also must be unique.

JSON example

				"rowId": 1,  "type" : "", 		
				"properties": {"username": "imam",  "publicId": "1", "description" : "something" }, 
				"indexes" :
						{"indexName": "usernameIndex", "fieldName" : "username", "indexValue" : "imam" }, 
						{"indexName": "publicIdIndex", "fieldName" : "publicId", "indexValue" : "1" }, 
						{"indexName": "descriptionIndex", "fieldName" : "description", "indexValue" : "something" } 
				"rowId": 2,  "type" : "", 	
				"properties": {"postcode": "b99"} 
				"rowId": 3,  "type" : "", 	
				"properties": {"name": "football", "description" : "something"},
				"indexes" :
						{"indexName": "hobbyDescriptionIndex", "fieldName" : "description", "indexValue" : "something" } 
				"rowId": 4,  "type" : "", 	
				"properties": {"name": "tennis", "description" : "something"},
				"indexes" :
						{"indexName": "hobbyDescriptionIndex", "fieldName" : "description", "indexValue" : "something" } 
			{ "fromRowId": 1, "toRowId": 2, "direction": "BOTH", "type": "ADDRESS", "properties": {"message": "home"} },
			{ "fromRowId": 1, "toRowId": 3, "direction": "BOTH", "type": "HOBBY" },
			{ "fromRowId": 1, "toRowId": 4, "direction": "BOTH", "type": "HOBBY" }

Points of interest:

  • rowId must always be provided and be unique. This is not stored in neo4j and used purely for mapping relationships.
  • If you're using SDN then the type property must be FQN.
  • Properties can be of any type, the example only show strings.
  • If you have indexes then the indexName must be unqiue and match the indexName specified in the entity.
  • In relationships the direction is not currently being used.

Example test

The following is how this should be used:

@ContextConfiguration({ "classpath:/spring/example-spring-context.xml" })
@TransactionConfiguration(transactionManager = "neo4jTransactionManager", defaultRollback = true)
@Neo4jTest(fileLocation = "json/test.json")
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class,
        Neo4jSeedTestExecutionListener.class })
public class SpringDataExampleTest
    UserRepository userRepo;

    HobbyRepository hobbyRepo;

    ... rest omitted

Key points

  • @Neo4jTest must be provided, fileLocation property should be available in the classpath.
  • Neo4jSeedTestExecutionListener.class should be added to the listeners.
  • The test data will be inserted and removed in the background (before class for insertion and after class for deletion).


  • This component was created in a few hours and is far from perfect. I've successfully consumed it for a simple SDN + Neo4j project, but am pretty sure it's not 100% yet.


Please feel free to contact me via email,