I am building an application using spring-boot (1.1.8.RELEASE), spring-data-neo4j (3.2.0.RELEASE) in order to connect to a stand alone neo4j server via rest api. I am using spring-test in order to test the application.
I would like to clean the database before running the unit test. I created a method cleanDb to be executed before to start the transaction. But I face a NullPointerException when cleanDb method from AppTests is run.
FYI when I removed this method all the unit-test pass.
Thank you for your help
Find below my spring configuration
@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableAutoConfiguration
public class AppConfig extends Neo4jConfiguration {
public AppConfig() {
setBasePackage("demo");
}
@Bean
public GraphDatabaseService graphDatabaseService(Environment environment) {
return new SpringRestGraphDatabase("http://localhost:7474/db/data");
}
}
Find below my test class
@SuppressWarnings("deprecation")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AppConfig.class)
@Transactional
public class AppTests {
@Autowired
private Neo4jTemplate template;
@Rollback(false)
@BeforeTransaction
public void cleanDb() {
Neo4jHelper.cleanDb(template);
}
@Test
public void templateTest() {
Person person = new Person();
person.setName("Benoit");
person.setBorn(1986);
Person newPerson = template.save(person);
Person retrievedPerson = template.findOne(newPerson.getNodeId(),Person.class);
Assert.assertEquals("Benoit", retrievedPerson.getName());
}
}
Find below the Failure trace when I execute the unit test:
org.springframework.data.neo4j.core.UncategorizedGraphStoreException: Error cleaning database ; nested exception is java.lang.NullPointerException
at org.springframework.data.neo4j.support.node.Neo4jHelper.cleanDb(Neo4jHelper.java:78)
at org.springframework.data.neo4j.support.node.Neo4jHelper.cleanDb(Neo4jHelper.java:67)
at org.springframework.data.neo4j.support.node.Neo4jHelper.cleanDb(Neo4jHelper.java:38)
at demo.AppTests.cleanDb(AppTests.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.runBeforeTransactionMethods(TransactionalTestExecutionListener.java:213)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:167)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:368)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NullPointerException
at org.neo4j.tooling.GlobalGraphOperations.assertInTransaction(GlobalGraphOperations.java:236)
at org.neo4j.tooling.GlobalGraphOperations.getAllNodes(GlobalGraphOperations.java:82)
at org.springframework.data.neo4j.support.node.Neo4jHelper.removeNodes(Neo4jHelper.java:88)
at org.springframework.data.neo4j.support.node.Neo4jHelper.cleanDb(Neo4jHelper.java:74)
... 30 more
)
Thanks
Regards