0

I'm new to unit testing . How to write unit test for these type of methods?

 private boolean fn(Vertex vertex) {
        return vertex.id().toString().split(":").length > 1;
    }

Here Vertex is the element of gremlin query . I have tried to create instance of graph and pass a new Vertex object to the function but not working . i.e

Vertex vertex = (Vertex) graphTraversalSource.addV("Test").property(id,"Profile:TEST");

Can anyone suggest the ways to test these types of method?

Aman kumar
  • 83
  • 1
  • 7
  • Simple answer, you don't. You only write unit tests for public (or protected) methods. – Joakim Danielson Apr 11 '20 at 09:21
  • Does this answer your question? [How do I test a private function or a class that has private methods, fields or inner classes?](https://stackoverflow.com/questions/34571/how-do-i-test-a-private-function-or-a-class-that-has-private-methods-fields-or) – David Rawson Apr 12 '20 at 07:46

1 Answers1

1

You've asked your question about "unit testing" but your question really seems to be about why:

Vertex vertex = (Vertex) graphTraversalSource.addV("Test").property(id,"Profile:TEST");

doesn't let you create a Vertex that you can test. I'd say the most obvious problem is that you didn't iterate your traversal in any way. In this case you need to call next():

Vertex vertex = (Vertex) graphTraversalSource.addV("Test").property(id,"Profile:TEST").next();

Of course, for your fn(Vertex) function that you want to test, I don't see much point in creating an actual Vertex in a graph database - you could instead just use org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex and do:

Vertex vertex = new DetachedVertex("Profile:TEST", "Test", null);

and then pass that to your function to test.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135