Search code examples
javaunit-testingmockitotinkerpop

How to create customised vertex without creating the graph.?


I need to make an object of vertex in apache.tinkerpop for unit testing without creating an actual graph . Basically , I see no point to create an actual vertex in graph database.

I read about detached vertex , but not sure how to use it . Is there any other way to do this?


Solution

  • The DetachedVertex class utilizes a builder pattern to instantiate instances - see javadocs. Use the static build() method to create a Builder instance and then set its properties before calling create() to instantiate an instance:

    Vertex v = DetachedVertex.build().setId(1).setLabel('person').create();
    

    In the above example, we create a Vertex with a unique identifier and label but no properties. Adding properties is accomplished using the addProperty() method which takes aDetachedVertexProperty- see [javadoc][2]. LikeDetachedVertex, theDetachedVertexProperty` uses a builder pattern, therefore adding properties is like:

    Vertex v = DetachedVertex.build().setId(1).setLabel('person').
                   addProperty(DetachedVertexProperty.build().                                    
                                                      setId(10).setLabel('name').
                                                      setValue('marko').create()).
                   create();