1

I am running javers in a spring-boot application with mongodb. Autoconfiguration works fine and it is really easy to use.

I am now trying to clean the javers collection on test setups like this:

mongoTemplate.db.getCollection("jv_snapshots").deleteMany(new Document())
mongoTemplate.db.getCollection("jv_head_id").deleteMany(new Document())

I have several tests that, for convenience of object creation simplification, have the same fixed object id, which might cause the following issue:

When i duplicate a simple test method

 /**
 * [Test worker] INFO  org.javers.core.Javers - Commit(id:1.00, snapshots:1, author:unauthenticated, changes - NewObject:1), done in 101 millis (diff:49, persist:52)
 */
@Order(0)
def "create entity to induce error on second test"() {
    def entity = repo.save(dummyEntityWithFixedId())
    def changes = javers.findChanges(QueryBuilder.byInstance(entity).build())

    expect:
    changes.size() == 1
}

/**
 * [Test worker] INFO  org.javers.core.Javers - Skipping persisting empty commit: Commit(id:1.01, snapshots:0, author:unauthenticated, changes -)
 * [Test worker] INFO  org.javers.core.Javers - Commit(id:1.01, snapshots:0, author:unauthenticated, changes -), done in 4 millis (diff:4, persist:0)
 */
@Order(1)
def "fails on all test run because previous test is not cleaned correctly"() {
    def entity = repo.save(dummyEntityWithFixedId())
    def changes = javers.findChanges(QueryBuilder.byInstance(entity).build())

    expect:
    changes.size() == 1
}

As i put the log output produced in javadoc, you can see that the second test run somehow detects an old version and tries to update it.

Is there any caching done by javers that i am missing? As i understand the documentation, the standard behaviour is synced commits. I am aware that i can avoid the error if i do not fix the entity's ids, or if i annotate the tests with @DirtiesContext to force a spring context refresh between tests, but this got me curious.

I forked the javers repo and added this test to it, you can find it here: https://github.com/cgoege/javers/blob/master/javers-spring-boot-starter-mongo/src/test/groovy/org/javers/spring/boot/mongo/FailOnMultipleTests.groovy

Thanks in advance for an answer.

cgo
  • 11
  • 1
  • Would rolling back the transaction after each test help? https://stackoverflow.com/questions/12626502/rollback-transaction-after-test asks (and answers) a similar question. – Zephyr Sep 08 '22 at 11:32

0 Answers0