Question

I am unit testing domain logic and domain objects backed by a neo4j database. Most of these tests need to mock the neo4j GraphDatabaseService, various Nodes, and various Relationships. Some of the mocked methods return these mocked objects. For example, a getReferenceNode() call returns a mocked Node or a getSingleRelationship() call returns a mocked Relationship whose getEndNode() in turn returns a mocked Node.

I'm concerned by the number of mocks returning mocks returning mocks. Usually, this isn't recommended. It certainly complicates the test setup and leads to quite brittle tests, because so many layers of neo4j functionality need to be mocked.

Is there a way to avoid this when unit testing neo4j-backed domain logic?

Was it helpful?

Solution

You could try using a temporary database--one that gets created/flushed every time. In case you need to sample data then you could:

  1. either have a fixture that populates the new db with data;
  2. have a test db setup that is used every time you run tests (in this case you must figure out a way to rollback your changes or always start from the known state)

OTHER TIPS

I am using Maven, Spring data source and unit test my app using the ImpermanentGraphDatabase. Since it was quite hard to set it up here is what i did:

in my applicationContext.xml I initialized the graphDatabaseService:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
    default-lazy-init="true">


    <neo4j:config graphDatabaseService="graphDatabaseService"/>
    <!--  use in memory graph database -->
    <bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase"/>

</beans>

in my pom.xml i had to add the kernel-tests:

    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-kernel</artifactId>
        <version>1.6</version>
        <classifier>tests</classifier>
        <scope>test</scope>
    </dependency>

otherwise the impermanentGraphDatabase won't be available.

finally I could use a clean graph db evrytime:

public class MyNeo4JTest extends TestCase {

    protected ApplicationContext ctx;
    protected GraphDatabaseService gds;

    @Before
    public void setUp() throws Exception {

        // test-data
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        gds = ctx.getBean(GraphDatabaseService.class);
    }

    @Test
    public void testUser () {
          ...
    }
}

I find that the setup is MUCH faster than using the normal way. keeping everything in memory seems to pay off

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top