Announcement Announcement Module
Collapse
No announcement yet.
[neo4j] Index creation fails when running multiple unit tests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [neo4j] Index creation fails when running multiple unit tests

    Hi All,

    We are having problem to run our unit tests (which uses impermanent graph database) from a gradle build script. Note that there are now quite a number of Unit test case files that perform database related tests.

    In the application context file we are using for our unit tests, the impermanent database is defined in the following way

    <bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase"
    destroy-method="shutdown">
    </bean>
    <neo4j:config graphDatabaseService="graphDatabaseService" />


    Each of our unit test classes were annotated using @DirtiesContext(classMode = AFTER_CLASS) to make sure we get a clean application context per unit test.

    The unit test classes belong to a particular group which is being executed from a gradle build script.
    When we run the gradle script we expect each of the unit test will use a fresh application context and so a fresh instance of ImpermanentGraphDatabase.
    But the build script fails and stack trace shows that in one of the test methods where I am trying to retrieve the object from repository (i.e., A a = repos.findByPropertyValue("name", A_NAME) ), it is trying to create an index internally and it fails. The stack trace starts like the following

    org.neo4j.graphdb.TransactionFailureException: Index creation failed for AgentHostDTO, {provider=lucene, type=exact}"
    org.springframework.data.neo4j.support.mapping.Abs tractConstructorEntityInstantiator.createEntityFro mState(AbstractConstructorEntityInstantiator.java: 61)
    org.springframework.data.neo4j.support.mapping.Neo 4jEntityPersister$CachedInstantiator.createEntityF romState(Neo4jEntityPersister.java:135)
    ..............


    What can be the possible reason for this index creation failure ?
    Even if the unit test uses the same database instance (which we do not want to), the index creation should not fail. Am I correct ?.

  • #2
    faisala,

    Neo4jHelper might be your friend: https://github.com/SpringSource/spri...elper.java#L62

    I use it thus:

    Code:
        @BeforeTransaction
        public void beforeTransaction() {
            Neo4jHelper.cleanDb(graphDatabaseService, true);
        }
    Regards,

    Lasse

    Comment


    • #3
      Thanks Lasse,

      My user scenario is there are two or three methods per unit test class. Each methods are dependent on each other (used dependsOnGroups option) and follow a sequence of execution. The cleanDb needs to be done before executing the very first test in a class.

      So, what I have been doing something similar to what you said

      @BeforeClass
      public void beforeTransaction() {
      Neo4jHelper.cleanDb(graphDatabaseService, true);
      }

      But the above code will solve my problem if all the test "classes" running from gradle script runs sequentially.
      It seems like when you have dependencies among the "methods" inside a class, testNG does not execute the "classes" one at a time. It executes different "methods" from different "classes" following a order it thinks is optimized. (note we don't want to use any hard coded testNG xml to show the order of the classes)
      So, we should make sure each test class gets a different instance of impermanent database, so there is not conflict.
      If I try to print out impermanent datatabse instance from different classes, it looks like they are using the same database from different threads, which is the root to the problem.

      So questions are:
      How do we make sure we use different instances of impermanent database from unit tests ?
      Even they use the same instance of the database, why the index creation will fail ?
      (may be not related to this forum) Is there any way to execute the unit test classes sequentially (when they have interdependent methods inside them) ?


      Thanks
      Faisal

      Comment


      • #4
        Originally posted by faisala View Post
        It seems like when you have dependencies among the "methods" inside a class, testNG does not execute the "classes" one at a time
        Faisal,

        Indeed, I had a similar problem once, it is a feature of TestNG and actually it helps you to keep your tests isolated from eachother - but it can be a bit painful...

        If I were you, I would give JUnit a try, that is what we use at Neo4j and I have not seen the problem you have.

        - Lasse

        Comment


        • #5
          Thanks Lasse!

          I will try and let you know.

          Faisal

          Comment

          Working...
          X