Benoit D'Incau
Benoit D'Incau

Reputation: 11

Spring Data Neo4j - Unit Test - Transaction rollbacked but data not deleted

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 have implemented a unit test to create a Node and retrieved it. It is working well but the new node remained in the database after the test is completed, however I expect the transaction to be rollbacked and the node deleted

However in the console I can see the following statement.

"Rolled back transaction after test execution for test context...

** I don't understand why based on the console the roll back seems to have occured but the transaction has been committed to the database. **

It would be really appreciated if somebody could help me to figure out where the issue is coming from.

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;

@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());
}

}

I tried to add the following annotation in my unit test class but it did not change anything:

@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)

I also tried to add the following in my unit test based on what I have seen in other posts

implements ApplicationContextAware

Thank you for your help

Regards

Upvotes: 1

Views: 551

Answers (1)

Sam Brannen
Sam Brannen

Reputation: 31177

The behavior you are experiencing is to be expected: there is nothing wrong with transaction support in the Spring TestContext Framework (TCF) in this regard.

The TCF manages transactions via the configured transactionManager.

So when you switched to an embedded database and configured the transaction manager with the data source for that embedded database, that works perfectly. The issue is that the transaction support in Neo4J-REST does not tie in with Spring's transaction management facilities. As Michael Hunger stated in the other thread you referenced, an upcoming version of the Neo4J-REST API should address this issue.

Note that annotating your test class with @TransactionConfiguration has zero effect since you are merely overriding the defaults with the defaults which achieves nothing. Furthermore, implementing ApplicationContextAware in a test class has no effect on transaction management.

Regards,

Sam (spring-test component lead)

Upvotes: 1

Related Questions