I have a Neo4j 5.26 database with graph (minimized example):
CREATE (x1:L {id:1})-[:R]->(x2:L {id:2})-[:R]->(x3:L {id:3})
-[:R]->(x4:L {id:4})-[:R]->(x5:L {id:5})
and some algorithm in Traversal framework which traverses part of it and returns some result:
Node x1 = tx.findNode(Label.label("L"), "id", 1);
Traverser traverser = tx.traversalDescription()
.depthFirst()
.uniqueness(Uniqueness.NODE_PATH)
.relationships(RelationshipType.withName("R"))
.evaluator(Evaluators.toDepth(2)) //*
.traverse(x1);
for (Path path : traverser) {
// yields 1, 1->2, 1->2->3
}
The algorithm does some exploration of the area between node 1 and 3. It should never exceed beyond node 3. The result is built from some subset of nodes 1 to 3 as well.
Everything works OK. Now, I am composing a test suite for this algorithm. My question is: how to test that nodes 4 and 5 weren't reached at all by traversal algorithm?
Obvious test for absence of node 4 in result is not enough because algorithm might have traversed broader area than area from which the result is effectively generated. For example, I want the test to fail if someone changes evaluator in //*-marked line to
.evaluator(path -> path.length() <= 2 ? INCLUDE_AND_CONTINUE : EXCLUDE_AND_CONTINUE)
even though the result is same (note Evaluators.toDepth prunes beyond length threshold).
I am aware the purpose of tests is primarily to test result rather than implementation hence my requirement is somewhat dirty (and probably the solution as well - if any exists). The rationale of my requirement is to ensure that algorithm does not get out of control under all circumstances (actually the area of nodes 4 to 5 is large and leaking traversal into it causes drastic slowdown).
Some ideas came to my mind (to make the NodeEntity instance of node 4 to somehow throw exception on getId() or other method used by Traversal framework) but without clue how to drill down into Neo4j internals.
I am hesitant to do changes in production code only to adjust to test, for example to expose part of snippet of traverser creation to test. On the other hand I don't a priori refuse any heavyweight or hacky solution based on reflection, Power mock, class bytecode fiddling etc., to certain level of meaningfulness.